Data Type:
Data type is derived from System.ValueType. The value type contains some data. So, before declaring a variable we write the type of the variable and it is called datatype. According to these datatypesthe system allocates memory to the variables. The types of datatypes are:
Data Type
|
Allocated memory
|
Range
|
int
|
4 byte
|
-2,147,483,648 to 2,147,483,647
|
short
|
2 byte
|
-32,768 to 32,767
|
long
|
8 byte
|
-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
|
uint
|
4 byte
|
0 to 4,294,967,295
|
ushort
|
2 byte
|
0 to 65,535
|
ulong
|
8 byte
|
0 to 18,446,744,073,709,551,615
|
float
|
4 byte
|
-3.4 x 1038 to + 3.4 x 1038
|
double
|
8 byte
|
(+/-)5.0 x 10-324 to (+/-)1.7 x 10308
|
decimal
|
16 byte
|
(-7.9 x 1028 to 7.9 x 1028) / 100 to 28
|
byte
|
1 byte
|
0 to 255
|
sbyte
|
1 byte
|
-128 to 127
|
char
|
16-bit Unicode character
|
U +0000 to U +ffff
|
string
|
A string of Unicode characters.
|
|
bool
|
1 byte
|
True or false
|
Examples of data types:
- int a=23; //Allocated 4 bytes
- float c= 3.45; //Allocated 4 bytes
- decimal d=0.555555555; // Allocated 16 bytes
- bool h= true;
- char c =’A’;
- string greetings =”Hi all”;
- double = 4.33333365; // Allocated 8 bytes
- short = 2; // Allocated 2 bytes
Type Conversion:
Data type can be converted into similar types. And this conversion is called casting. It is of two types. They are:
- Implicit casting:
Data types can be converted implicitly as well as explicitly. Implicitly, the conversion is done by the compiler but there is threat of losing data. It is also called implicit casting. In implicit casting, no external cast function is written.
Example:
Int x;
Double y= x;
Here, x is integer in start but it is changed into double without writing any casting function.
- Explicit casting:
Explicitly, the conversion is done by using casting to avoid threat of losing data. In explicit casting, external predefined function is written. They need a cast operator.
Example:
double y = 123;
int x = (int)y;
Here, y is double in start but it is explicitly changed into integer by writing cast function.
There are different type Conversion methods. They are,
1
|
ToBoolean
|
Converts a type to a Boolean value, where possible.
|
2
|
ToByte
|
Converts a type to a byte.
|
3
|
ToChar
|
Converts a type to a single Unicode character, where possible.
|
4
|
ToDateTime
|
Converts a type (integer or string type) to date-time structures.
|
5
|
ToDecimal
|
Converts a floating point or integer type to a decimal type.
|
6
|
ToDouble
|
Converts a type to a double type.
|
7
|
ToInt16
|
Converts a type to a 16-bit integer.
|
8
|
ToInt32
|
Converts a type to a 32-bit integer.
|
9
|
ToInt64
|
Converts a type to a 64-bit integer.
|
10
|
ToSbyte
|
Converts a type to a signed byte type.
|
11
|
ToSingle
|
Converts a type to a small floating point number.
|
12
|
ToString
|
Converts a type to a string.
|
13
|
ToType
|
Converts a type to a specified type.
|
14
|
ToUInt16
|
Converts a type to an unsigned int type.
|
15
|
ToUInt32
|
Converts a type to an unsigned long type.
|
16
|
ToUInt64
|
Converts a type to an unsigned big integer.
|
Example:
- using System;
- namespace TypeConversionApplication
- {
- class StringConversion
- {
- static void Main(string[] args)
- {
- int i = 5;
- float f = 53.0 f;
- double d = 235.75442;
- bool b = true;
- Console.WriteLine(i.ToString());
- Console.WriteLine(f.ToString());
- Console.WriteLine(d.ToString());
- Console.WriteLine(b.ToString());
- Console.ReadKey();
- }
- }
- }
After execution the output generated will be:
5
53.0
235.75442
True
I hope this blog will help you in learning data types and their conversion. Please do comment and give your valuable feedback if needed.