The data type tells a C# compiler what kind of value a variable can hold. Data type defines the type of data entered by the user. In C#, data types are categorized into 3 categories:
A data type is a value type if it holds a data value directly. For example: int i=10;. They are derived from the class System.ValueType. Value type again categorized into 3 main categories:
Integer Data Type contains only numbers. Decimal numbers are not allowed. Integer data type is further classified into different categories listed in the below table:
S.No. | Type | Aliased | Signed/Unsigned | Suffix | Size | Range |
---|---|---|---|---|---|---|
sbyte | System.SByte | Signed | None | 1 byte | -128 to 127 | |
byte | System.Byte | Unsigned | None | 1 byte | 0 to 255 | |
short | System.Int16 | Signed | None | 2 bytes | -32768 to 32767 | |
ushort | System.UInt16 | Unsigned | None | 2 bytes | 0 to 65535 | |
int | System.Int32 | Signed | None | 4 bytes | -2147483648 to 2147483647 | |
uint | System.UInt32 | Unsigned | U or u | 4 bytes | 0 to 4294967295 | |
long | System.Int64 | Signed | L or l | 8 bytes | -263 to 263-1 | |
ulong | System.UInt64 | Unsigned | Ul,UL,ul, LU,lU,Lu | 8 bytes | 0 to 264-1 |
Floating-point data types are used to store decimal values. Floating point is further classified into different categories.
S.No. | Type | Aliased | Suffix | Size | Range |
---|---|---|---|---|---|
float /single | System.Single | F or f | 4 byte | 1.5 x 10-45 to 3.4 x 1038 with precision of 7 digits | |
Double | System.Double | None, d or D | 8 byte | 5.0 x 10-324 to 1.7 x 10308 with precision of 15-16 digits | |
Decimal | System.Decimal | M or m | 16 bytes | 1.0 x 10-28 to 7.9 x 1028 with precision of 28-29 digits |
Character Data Type is used to store alphabet, number, special character.
S.No. | Type | Aliased | Suffix | Allowed Values |
---|---|---|---|---|
Char | System.Char | None | Single Unicode character, stored as integer between 0 to 65535 | |
Bool | System.Boolean | None | Boolean values, true or false, Default value is false. | |
DateTime | System.DateTime | 8 bytes | 0:00:00am 1/1/01 to 11:59:59pm 12/31/9999 |
Unlike value types, a reference type doesn't store its value directly. Instead, it stores the address where the value is being stored. For example: string s = "Hello World"; Reference types have a null value by default, when they are not initialized.
Type | Aliased | Suffix | Allowed Values |
---|---|---|---|
String | System.String | None | A sequence of characters |
Arrays | None | Collection of values | |
Class | None | ||
Delegates | None |
The Object Type is the base class for all data types. In object data types any type of value can be assigned.
GetType(): It returns the Type object of current instance. object obj1 = 4; object obj2 = "Test"; string type1 = obj1.GetType().ToString(); // returns System.Int32 string type2 = obj2.GetType().ToString(); // returns System.String.
S.No. | Value type | Reference type |
---|---|---|
Value type stores the value of their data. | Reference types store the address of their data. | |
In value type, creating new objects occupy the different memory location. | In Reference type, creating a new object does not occupy a new memory location rather it shares the same memory location of existing objects. | |
In value type change in one object does not reflect other object. | In Reference type, change of value in one object reflects another object. |