C# Data Types

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:

Value Type

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

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. TypeAliased Signed/UnsignedSuffixSize Range
sbyteSystem.SByte SignedNone1 byte-128 to 127
byteSystem.Byte UnsignedNone1 byte0 to 255
shortSystem.Int16 SignedNone2 bytes-32768 to 32767
ushortSystem.UInt16 UnsignedNone2 bytes0 to 65535
intSystem.Int32 SignedNone4 bytes-2147483648 to 2147483647
uintSystem.UInt32 UnsignedU or u4 bytes0 to 4294967295
longSystem.Int64 SignedL or l8 bytes-263 to 263-1
ulongSystem.UInt64 UnsignedUl,UL,ul,
8 bytes 0 to 264-1

Floating-point Data Type

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

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

Reference Type

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

Object Type

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.
Comparing Value type and Reference type
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.