Lesson 2 – Data Types in C#


In the last lesson, you saw how to set up the C# environment and how to install your first C# program. In this lesson, you will study the concept of data types and variables in C#. A variable is basically placeholder that is used to store data of a particular data type. A variable points to the memory location where the actual data is stored.

C# datatypes can be broadly categorized into four types:

  1. Numeric
  2. Boolean
  3. String
  4. Char

Numeric Data Type

Numeric data types in C# are used to store numeric data. The following table shows commonly used numeric data types supported by C#:

Data TypeDefault ValueSize
int04 byte
signed int04 byte
unsigned int04 byte
long08 byte
signed long08 byte
unsigned long08 byte
float0.0F4 byte
double0.0D8 byte
decimal0.0M16 byte

Let’s now see a very simple example that uses some of these numeric operators:

using System;

namespace C_Project
{
    class Program
    {
        static void Main(string[] args)
        {
            int number1 = 100;
            int number2 = 50;

            float number3 = 50.56F;
            float number4 = 50.62F;

            Console.WriteLine(number1 + number2);
            Console.WriteLine(number3 + number4);
        }
    }
}

In the script above, we declare two integer variables number1 and number2 which store integer type data 100 and 50. Similarly, we declare two floating variables number3 and number4 with values 50.56F and 50.62F respectively. Next, we add variable number1 with number2 and the variable number3 with number4. Finally, we display the result of the addition on the console. The output looks like this:

150
101.18

Boolean Data

The boolean data type is used to store boolean data. In C#, the word “bool” is used to store boolean data. A “bool” type variable can have two values: True or False. The following example will further clarify the use of the boolean variable.

using System;

namespace C_Project
{
    class Program
    {
        static void Main(string[] args)
        {
            bool sick = false;

            Console.WriteLine("Are you sick?: Anser =" + false);
        }
    }
}

In the script above, a boolean variable “sick” is declared with value “false”. Next, a statement is printed on the console which asks questions from the user if he is sick. The reply is the value of the variable “sick”.

Strings

Strings in C# are used to store textual data such as names of the entities and the dialogue box messages. The following example demonstrates the use of strings in C#.

using System;

namespace C_Project
{
    class Program
    {
        static void Main(string[] args)
        {
            string message1 = "Welcome to Dronaschool.com";
            Console.WriteLine(message1);
        }
    }
}

In the script above, we declare a string variable “message1” and store a message “Welcome to Dronaschool.com” in this variable. Next, we simply print the value of the “message1” variable on the screen. The output of the above script looks like this:

Welcome to Dronaschool.com

Characters

Characters, as the name suggests are used to store character type data. For instance, if you want to store a character “a”, you can use a character type variable as shown in the following example:

using System;

namespace C_Project
{
    class Program
    {
        static void Main(string[] args)
        {
            char a = 'a';
            Console.WriteLine(a);
        }
    }
}

What’s Next?

In this lesson, you studied C# data types. In the next lesson, you will see C# operators.