Data Type And Variables

A variable is nothing but a data store in which we store data that we are going to use in our application and data type is a term which describe what type of data you are storing in that variable.
So in order to store data in a variables, we first have to declare that variable and also declare the type of data we are storing in it i. e. we have to specify the data type for our variables declared.

below is variable declaration syntax and example.

[Data Type name] [variable name]

+
int i;
i = 123;
 
string j;
j = "Testing";

There are various built-in data types available in dot net framework. We can see in our above example 2 data type samples int in which we store only integer values and string in which we can store only string values. We can build our own custom data type as well.
Now Compiler uses this information to make sure that all the operations/assignments that are going to happen in your code are type safe and hence we are saying that C# is strongly typed language. Meaning that we can store only integer values into variable whose data type is int. If we are going to store values other than int, it will gives us error or inconsistent result.
For example if I am going to make addition of above variables i and j and storing it in another int variable k, then compiler will give error Cannot implicitly convert type 'string' to 'int' 

+
int k;
k = i + j;

Below table gives list of Data types present in dot net framework with their Size limit. If we store data beyond the size limit then compiler will give an error.

Data TypeSize
sbyte-128 to 127
byte0 to 255
charU+0000 to U+ffff
short-32,768 to 32,767
ushort0 to 65,535
int-2,147,483,648 to 2,147,483,647
uint0 to 4,294,967,295
long-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
ulong0 to 18,446,744,073,709,551,615
float±1.5e−45 to ±3.4e38
double±5.0e−324 to ±1.7e308
string0 to approx. two billion (2 ^ 31) Unicode characters

You can check each data type size limit by using MaxValue and MinValue properties of data type. for example if you want to check size of int data type then simply write int.MaxValue for getting maximum value and int.MinValue for minimum value in your code.

No comments :