What is a data type ?
Data types or variable are an important thing in any programming language. It is the basic starting point for a programmer. It generally tells to the compiler the type of value it can hold.
In below example we have declared a string variable in C#.net as. String s = "Hello" . It tells to the compiler that string is a data type that holds a text value.
int i = 123
In above example, we have declared a integer type in C#.net. It tells to the compiler that
int is a type that will be used to hold numbers.
Data types has some specific size and length. If we declare a type and at run time if the value it holds exceeds the max limit that the type can hold then we get the data overflow issues. So its very important to have proper data types in our program.
In below example.
We have declared a type of an int and assigned max integer value to it. During initialization i holds value as 2147483647 (max of integer value) . We have another variable declared as k, this data type assigned the value of 2147483647 * 100 at run time.
In-other words we are trying to assign variable k to hold a value 214748364700 which is out side of the upper limit of int at run-time. “Overflow Exception” is thrown when we run this program.
Data Type in C# details :
Below table lists the data types available in C# along with their range and size in byte.
|Data type||Size(in byte)||Range||Description|
|byte||1||0 to 255||Unsigned 8-bit|
|sbyte||1||-128 to 127||Signed 8-bit|
|short||2||-32,768 to 32,767||Signed 16-bit|
|ushort||2||0 to 65,535||Unsigned 16-bit|
|int||4||-2,147,483,648 to 2,147,483,647||Signed 32-bit|
|uint||4||0 to 4294967295||Unsigned 32-bit|
|long||8||-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807||Signed 64-bit|
|ulong||8||0 to 18,446,744,073,709,551,615||Unsigned 64-bit|
|float||4||-3.402823e38 to 3.402823e38||32-bit floating point|
|double||8||-1.79769313486232e308 to 1.79769313486232e308||64-bit floating point|
|object||4 byte address||Base type for all types|
|char||2||Unicode characters||16-bit Unicode character|
|string||4 byte address||Array of character|
|decimal||24||(+ or -)1.0 x 10e-28 to 7.9 x 10e28||128-bit floating value|
|bool||1||True, False||true/false value|
|DateTime||0:00:00am 1/1/01 to
|Represents date and time value|
|Dynamic||4 byte address||Identical to object|
All types in C#.Net are derived directly or indirectly from System.Object. So by default all data types in C#.Net supports the methods that are present in System.Object. Below methods are by default derived in all types.
- Equals – Determines if two objects are equal.
- GetHashCode – Returns default hash function.
- ToString – Returns a string that represents current object.
- GetType – Returns current type.
In the above example, data type string and int has the default methods as derived from System.Object.