# C# Variables and Data Types

You have learned to take the take input and print anything on the screen. It's time to move forward and learn something new.

Let's try this example first:

using System;

class Test
{
static void Main(string[] args)
{
int n;
n = 4;
Console.WriteLine(n);
}
}

4

In this example, n is a variable. Variables are used to store certain values. In our case, we stored a value of 4 to the variable n. Let's understand the above example.

int n → This statement declares that variable 'n' can store some integer value. Whenever a variable is declared, it occupies some space in the memory of a computer. With this declaration, a space will be allocated to n in the memory of the computer to store an integer value.

n = 4 → We are assigning a value 4 to the variable 'n'.

Console.WriteLine(n) → We use the WriteLine function to print the value of n to the screen.

Note that n is not written inside inverted commas (" "). n written inside inverted commas (" ") would have printed simple 'n' instead of the value of n. For example, we used Console.WriteLine("Hello World") to display "Hello World" on the screen in the previous chapter.

Let's look at one more example.

using System;

class Test
{
static void Main(string[] args)
{
int n;
n = 4;
Console.WriteLine($"n is {n}"); } }  n is 4 If we add $ before ", the value of the variable written inside {} is evaluated and printed. Thus, in the above example, the value of n i.e., 4 got printed in place of {4}.

using System;

class Test
{
static void Main(string[] args)
{
Console.WriteLine(\$"Sum of 2 and 3 is {2+3}");
}
}

Sum of 2 and 3 is 5

In this example also, the value got evaluated first and then 5 got printed instead of {2+3}.

We can also declare multiple variables at once and it is totally fine with C#.

using System;

class Test
{
static void Main(string[] args)
{
int a, b, c, d, e;
a = 3;
}
}


In the above example, we have declared 5 variables at once - int a, b, c, d, e;.

We can also initialize variables at the time of declaration. Look at the following example.

using System;

class Test
{
static void Main(string[] args)
{
int a = 3, b, c = 2, d = 5, e;
b = 9;
e = 12;
Console.WriteLine(a+b+c+d+e);
}
}

31

Till now, we have only used integers. We can also have variables of different types like decimal, boolean, etc. which we are going to study next.

## C# Data Types

Variables can be of different types depending on the type of data it can store. In the last few examples, all variable were declared as int and thus can store integer values. Similarly, a variable which stores a character value is of type char.

We specify the type of a variable at the time of declaration. For example, a character variable is declared as shown below.

char ch;

In the above declaration, ch is the name of the variable which is of type char i.e., it can store character values. Let's see an example to print a character value.

using System;

class Test
{
static void Main(string[] args)
{
char ch;
ch = 'b';
Console.WriteLine(ch);
}
}

b

Here ch is the name of a variable of type char which is given a character value 'b'. It could take any character value like 'a', 'b', 'c', etc. Note that the character value is written within ' '. Characters are written inside ' '.

Similarly, there are other data types like float (number having decimal), double (number having decimal) and boolean (true or false). Each of these data types will use some memory while using them and the memory allocated is different for different data types. The size of the memory allocated also depends upon the computer i.e., it will be different for a 32 bit OS and a 64 bit OS for the same data type.

The range of values which these data types can take is also different. For example, an int can take values from -2,147,483,648 to -2,147,483,647. Whereas, a long can take values from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. But the memory used by a long will also be more than that of an int.

Let's look at the table given below mentioning range and memory used for each data type for a 32-bit system.

Type Keyword Size (byte) Range of Value
Integer int 4 -2,147,483,648 to 2,147,483,647
Floating point float 4 -3.4E+38 to 3.4E+38 (7-digit precision)
Double floating point double 8 -1.7E+308 to 1.7E+308 (15-digit precision)
Character char 1 –128 to 127
Boolean bool 1 false or true
Unsigned Integer uint 4 0 to 4,294,967,295
Short Integer short 2 -32,768 to 32,767
Unsigned Short Integer ushort 2 0 to 65,535
Long Integer long 8 -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
Unsigned Long Integer ulong 8 0 to 18,446,744,073,709,551,615

The sizes of variables might change depending on the compiler and the computer you are using.

Thus, we can store any type of data in a variable by declaring its datatype. Let's see an example.

using System;

class Test
{
static void Main(string[] args)
{
int a = 4;
float b = 2.2f;
double c = 2.54;
char d = 'a';
bool e = true;
uint f = 43;
short g = 2;
ushort h = 19;
long i = 123;
ulong j = 34;

Console.WriteLine(a);
Console.WriteLine(b);
Console.WriteLine(c);
Console.WriteLine(d);
Console.WriteLine(e);
Console.WriteLine(f);
Console.WriteLine(g);
Console.WriteLine(h);
Console.WriteLine(i);
Console.WriteLine(j);
}
}

4
2.2
2.54
a
True
43
2
19
123
34

Here we declared variables of types int, float, double, char, boolean, etc. and stored respective values in these.

One thing to note here is that we have added f after the value of the float because, by default, the decimal value at the right side of = is treated as double. Thus to initialize a float value, we use f or F after the value.

### C# MaxValue and MinValue

We can also get the maximum and minimum values of a data type using MaxValue and MinValue respectively. Let's look at the example given below.

using System;

class Test
{
static void Main(string[] args)
{
Console.WriteLine(int.MinValue);
Console.WriteLine(int.MaxValue);

Console.WriteLine(); // to print new line

Console.WriteLine(float.MinValue);
Console.WriteLine(float.MaxValue);

Console.WriteLine(); // to print new line

Console.WriteLine(double.MinValue);
Console.WriteLine(double.MaxValue);

Console.WriteLine(); // to print new line

Console.WriteLine(uint.MinValue);
Console.WriteLine(uint.MaxValue);

Console.WriteLine(); // to print new line

Console.WriteLine(short.MinValue);
Console.WriteLine(short.MaxValue);

Console.WriteLine(); // to print new line

Console.WriteLine(ushort.MinValue);
Console.WriteLine(ushort.MaxValue);

Console.WriteLine(); // to print new line

Console.WriteLine(long.MinValue);
Console.WriteLine(long.MaxValue);

Console.WriteLine(); // to print new line

Console.WriteLine(ulong.MinValue);
Console.WriteLine(ulong.MaxValue);
}
}

-2147483648
2147483647

-3.402823E+38
3.402823E+38

-1.79769313486232E+308
1.79769313486232E+308

0
4294967295

-32768
32767

0
65535

-9223372036854775808
9223372036854775807

0
18446744073709551615

## C# Taking Numeric Inputs

We already know how to take normal string inputs. We will use WriteLine to take an input and then convert it into an int or double using different methods.

We can use Convert.ToInt32() and Convert.ToDouble() methods to convert a value to an integer and to a double respectively. Let's look at the following example.

using System;

class Test
{
static void Main(string[] args)
{
Console.WriteLine(Convert.ToInt32("2"));
Console.WriteLine(Convert.ToDouble("2.34"));
}
}

2
2.34

We can also do the same thing with the values taken from inputs from a user.

using System;

class Test
{
static void Main(string[] args)
{
int a;
double b;
string c;
string d;

a = Convert.ToInt32(c);
b = Convert.ToDouble(d);
Console.WriteLine(a);
Console.WriteLine(b);
}
}

12
2.34
12
2.34

In this example, we converted values taken into variables c and d to integer and double respectively using ToInt32 and ToDouble methods.

We can also write the above code as:

using System;

class Test
{
static void Main(string[] args)
{
}
}

12
2.34
12
2.34

Here, Console.ReadLine() took the input and the above code became equivalent to Console.WriteLine(Convert.ToInt32("12")); after giving "12" as input. After this, Convert.ToInt32("12") converted "12" (a string) to 12 (an integer). Finally, it became equivalent to Console.WriteLine(12) and 12 got printed.

### How is a character value stored in C#?

Whenever a character value is given to a variable of type char, its ASCII value (an integer value) gets stored (and not the character value).

We use (int)ch to print the integer value (ASCII value) of any character 'ch'.

Let's see an example to print the ASCII value of a character.

using System;

class Test
{
static void Main(string[] args)
{
Console.WriteLine((int)'a');
}
}

97

We used (int)'a' to print the ASCII code (97) of 'a'.

## C# Type Conversion

Type conversion is basically converting one data type into a different one. There is two types of conversions - implicit and explicit conversions.

### C# Implicit Conversion

Many times, C# converts a data type into a different one automatically and it is known as Automatic Type Conversion or Implicit Conversion. It is done when the conversion is safe and no data is lost during the conversion. For example, an integer 2 can be converted into a double 2.0 without loss of data. Let's look at some examples of implicit conversion:

using System;

class Test
{
static void Main(string[] args)
{
int a = 2;
long b = a; // implicit conversion

Console.WriteLine(b);
}
}

2
using System;

class Test
{
static void Main(string[] args)
{
int a = 2;
double b = a + 5.9; // implicit conversion

Console.WriteLine(b);
}
}

7.9

Look at the following table to get the list of data types which can be implicitly converted into different data types.

From To
sbyte short, int, long, float, double, or decimal
byte short, ushort, int, uint, long, ulong, float, double, or decimal
char ushort, int, uint, long, ulong, float, double, or decimal
short int, long, float, double, or decimal
ushort int, uint, long, ulong, float, double, or decimal
int long, float, double, or decimal
uint long, ulong, float, double, or decimal
long float, double, or decimal
ulong float, double, or decimal
float double

On the other hand, when the conversion is done by us manually, it is called Explicit Type Conversion. We can also convert a double 2.2 to an integer 2 but you can see it lead to loss of data so it will not be done automatically by C#.

We can write (datatype)value to convert the value to the datatype. For example, (int)2.7 will convert 2.7 to an integer 2 (floor value). Let's look at the following example.

using System;

class Test
{
static void Main(string[] args)
{
Console.WriteLine((int)2.2);
Console.WriteLine((float)5.3);
Console.WriteLine((int)'a');
Console.WriteLine((char)67);
}
}

2
5.3
97
C

Let's look at the table for possible explicit conversions.

From To
sbyte byte, ushort, uint, ulong, or char
byte sbyte or char
short sbyte, byte, ushort, uint, ulong, or char
ushort sbyte, byte, short, or char
int sbyte, byte, short, ushort, uint, ulong,or char
uint sbyte, byte, short, ushort, int, or char
long sbyte, byte, short, ushort, int, uint, ulong, or char
ulong sbyte, byte, short, ushort, int, uint, long, or char
char sbyte, byte, or short
float sbyte, byte, short, ushort, int, uint, long, ulong, char,or decimal
double sbyte, byte, short, ushort, int, uint, long, ulong, char, float,or decimal
decimal sbyte, byte, short, ushort, int, uint, long, ulong, char, float, or double

## C# Characters and Strings

Characters are enclosed within ' '. These can be letters (like 'a','s','z', '1', '2', etc.), escape sequence (like '\t') or some universal character.

There are some special sequence characters like '\t' and '\n' which we have already discussed in the previous chapter. Suppose, we get the need to print a character which is also part of the syntax of C# like ' ', " ", \, etc. We can easily print them by using \ before them. \ before special characters tells the compiler to treat them as normal characters. For example, \' will print ', \" will print ", \\ will print \ and \? will print ?.

A string is a collection of characters. In simple English, it is a letter, word, sentence or collection of sentences. There is an entire chapter on string in this course.

Strings are constants which are enclosed within " " like "Hello World", "C#", etc.

So, 'a' is a character but "a" is a string.

using System;

class Test
{
static void Main(string[] args)
{
Console.WriteLine("Hello");
Console.WriteLine("\\");
Console.WriteLine("\'");
Console.WriteLine("\"");
}
}

Hello
\
'
"

In the above example, \', \\, \" printed ', \ and " respectively.

### C# String Concatenation (+)

We can use + to join two string. Let's look at the following example.

using System;

class Test
{
static void Main(string[] args)
{
string a = "Hello";
Console.WriteLine(a + " CodesDope");
}
}

Hello CodesDope

Let's take one more example.

using System;

class Test
{
static void Main(string[] args)
{
string a = "Hello";
string b = "CodesDope";
string c = a+b;
Console.WriteLine(c);
}
}

HelloCodesDope

## C# var

For values like 5, 5.5, true, etc., we have an idea that these will be int, double and boolean respectively. Suppose we don't want to write the type of a variable during the declaration of a variable like int for int a = 5, we can use the var keyword before the variable and the compiler will implicitly assign a type to the variable. For example, var a = 5 will make a an int, var s = "Hello" will make s a string, etc.

Let's look at the following example.

using System;

class Test
{
static void Main(string[] args)
{
var a = 2;
var b = "CodesDope";
var c = 2.2;

Console.WriteLine(a);
Console.WriteLine(b);
Console.WriteLine(c);
}
}

2
"CodesDope"
2.2

However, we can't declare a variable as a var and initialize it later. For example, var a; will give us an error.

using System;

class Test
{
static void Main(string[] args)
{
var a;
}
}

file.cs(7,5): error CS0818: An implicitly typed local variable declarator must include an initializer

As stated earlier, the compiler allocates space for the variable according to the data type of the variable. For example, int a will allocate a space of one integer in the memory for the variable a. However, this is not possible with var a; because there is no idea for the amount of memory to be allocated for the variable a.

In this chapter, you learned about different data types and also converted one data type to a different one. Let's proceed to the next chapter to expand your programming knowledge.