What is a data type:
Data type in C# language is a container used for storing data.
Different Types of data types:
In C# language data types categorised into 2 types.
|Fixed size data types
||varying size data types
You can store any number of characters in string data type.
In Computers data types size is measured in bits,bytes etc..
How Data is stored during program execution:
During program execution all program related data is stored in RAM memory.
In the above diagram i is called as variable (variable is the name given to the data type or container).
Unsigned data types:
Unsigned data types stores only positive values including zero.
Rules for using C# data types:
1.Use lower case f or upper case F after decimal value while declaring float data type (otherwise compiler gives error).
Ex: float f=10.678f;
2.Use lower case m or upper case M after decimal value while declaring decimal data type(otherwise compiler gives error).
Ex: decimal dm1=10.45678m;
Conversions between data types:
- Conversion between compatable data types:
- Conversions between in-compatable data types:
Conversion between compatable data types:
For copying data from one integral data type(long,ulong,int,uint,short,ushort,byte,sbyte,char) to other integral data types or into float/double we must use implicit conversion or explicit conversion.
We must use implicit conversions while copying data from larger data type into smaller data type.
long l=i; //this code copies data from int data type into long data type.
The above conversion is called as implicit conversions since we are copying data from smaller data type into bigger data type.
Implicit conversion samples:
short s=10; int i=s;
byte b=10; short s=b;
byte b=10; long l=b;
int i=109; float f=i;
char c = 'a'; int i = c;
In the above sample code i will get 97 since a's ascii value is 97.
We must use explicit conversions while copying data from bigger data types into smaller data types. Following code uses explicit conversion for copying data from long type to int.
int i=l; //this code gives compilation error.
Since there is a possibility of loss of data while copying data from bigger data type(long) to smaller data type(int), Hence C# compiler will not accept the direct copying.
int i=(int)l; //this is explicit conversion .
The above given code will not produce any error (since we are explicitly saying to the C# compiler that programmer is ready to loose extra data while copying from bigger data type to smaller data type).
Explicit conversion samples:
we must use explicit conversions in all the following cases.
long l=100; int i=(int)l;
short s=100; byte b=(byte)s;
int i = 10; uint ui = (uint)i;
uint ui1 = 190;int i2 = (int)ui1;
Though float and int are having same capacity but still we must use explicit conversion for copying data from flot to int (since float can able to store decimal values also).
Using Convert Class Methods:
we must use convert class methods for copying data from one data type into another data type
when the source and destination data types are incompatable.
Ex: string and int are incompatable data types since string can able to store any type of data
where as int can able to store only intrgral values.
Convert class methods samples:
1)string s="1234"; int i=Convert.ToInt32(s);
2)string s="1234.567"; float f=Convert.ToSingle(s);
Right click on the destination data type and click on Go to Definition in visual studio code editor,for identifying the correct method for conversion(In case of string to float conversion type float in vs and right click on float , click on Go to definition option to see the actual underlying msil type for float).
Use visual studio intellisense while writing code (Ex. For identifying right method to copy data from string to float type Convert [dot] , then visual studio will show you list of methods available under Convert ).
Use convert class methods if noth implicit and explicit conversions are not possible.
1.Identify the maximum and minimum values which are allowed in the following data types.
a.int b.uint c.long d.ulong e.short f.ushort g.byte h.sbyte
In visual studio type data type name , right click on the data type and click on go to definition to observe max and min values.