In C#, data types are core fundamental elements which makes C# strongly typed language. This means all operation are typed checked by the compiler for type competibility. Because C# is strongly typed language, you required to tell to the compiler which data type you are using in programming. Both reference and value types are derived from the ultimate base class object.
C# has its own set of built in data types, which can be categories into value type and reference type. In C# 4 they introduced new data type called dynamic. Value type are used to hold actual value such as 3.44 or 2884, but reference type hold reference to the value. The most commonly used reference types are string or class data type.
Each data type in C# has corresponding underlying .NET Common Language Runtime type. For example, the int type in C# maps to the System.Int32 type in the runtime.
C# has following data types
Value type also known as simple type or primitive types which include boolean, byte etc. C# specifies specific range for each value type for example byte value type can hold value range from 0 to 255. All value types are derived implicitly from the System.ValueType.
C# has a set of predefined value types.
Data Type | Meaning | Alias For | Allowed Value |
---|---|---|---|
bool | Boolean value | System.Boolean | True or False |
byte | 8-bit unsigned integer | System.Byte | 0 to 255 |
char | 16-bit Unicode character | System.Char | U +0000 to U +ffff |
decimal | 128-bit precise decimal values with 28-29 significant digits | System.Decimal | (-7.9 x 1028 to 7.9 x 1028) / 100 to 28 |
double | 64-bit double-precision floating point type | System.Double | (+/-)5.0 x 10-324 to (+/-)1.7 x 10308 |
float | 32-bit single-precision floating point type | System.Single | -3.4 x 1038 to + 3.4 x 1038 |
int | 32-bit signed integer type | System.Int32 | -2,147,483,648 to 2,147,483,647 |
long | 64-bit signed integer type | System.Int64 | -923,372,036,854,775,808 to 9,223,372,036,854,775,807 |
sbyte | 8-bit signed integer type | System.Sbyte | -128 to 127 |
short | 16-bit signed integer type | System.Int16 | -32,768 to 32,767 |
uint | 32-bit unsigned integer type | System.UInt32 | 0 to 4,294,967,295 |
ulong | 64-bit unsigned integer type | System.UInt64 | 0 to 18,446,744,073,709,551,615 |
ushort | 16-bit unsigned integer type | System.UInt16 | 0 to 65,535 |
In C#, reference type hold the address of value instead of directly to a value. The reference type is store in heap and does not have fixed size, for example string can be size form 2 bytes to all the memory available on a system. Reference types include string, class, interfaces and delegates. C# has built-in reference types as: object, dynamic, and string.