C# - Data Types
C# is a strongly-typed language. It means we must declare the type of a variable which indicates the kind of values it is going to store such as integer, float, decimal, text, etc.
The following declares and initialized variables of different data types.
string stringVar = "Hello World!!";
int intVar = 100;
float floatVar = 10.2f;
char charVar = 'A';
bool boolVar = true;
C# mainly categorized data types in two types: Value types and Reference types. Value types include simple types (e.g. int, float, bool, and char), enum types, struct types, and Nullable value types. Reference types include class types, interface types, delegate types, and array types. Learn about value types and reference types in detail in the next chapter.
Predefined Data Types in C#
C# includes some predefined value types and reference types. The following table lists predefined data types:
Type | Description | Range | Suffix |
---|---|---|---|
byte | 8-bit unsigned integer | 0 to 255 | |
sbyte | 8-bit signed integer | -128 to 127 | |
short | 16-bit signed integer | -32,768 to 32,767 | |
ushort | 16-bit unsigned integer | 0 to 65,535 | |
int | 32-bit signed integer |
-2,147,483,648 to 2,147,483,647 |
|
uint | 32-bit unsigned integer | 0 to 4,294,967,295 | u |
long | 64-bit signed integer |
-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
l |
ulong | 64-bit unsigned integer | 0 to 18,446,744,073,709,551,615 | ul |
float | 32-bit Single-precision floating point type | -3.402823e38 to 3.402823e38 | f |
double | 64-bit double-precision floating point type | -1.79769313486232e308 to 1.79769313486232e308 | d |
decimal | 128-bit decimal type for financial and monetary calculations |
(+ or -)1.0 x 10e-28 to 7.9 x 10e28 |
m |
char | 16-bit single Unicode character | Any valid character, e.g. a,*, \x0058 (hex), or\u0058 (Unicode) | |
bool | 8-bit logical true/false value | True or False | |
object | Base type of all other types. | ||
string | A sequence of Unicode characters | ||
DateTime | Represents date and time |
0:00:00am 1/1/01 to 11:59:59pm 12/31/9999 |
As you can see in the above table that each data types (except string and object) includes value range. Compiler will give an error if value goes out of datatype's permitted range. For example, int data type's range is -2,147,483,648 to 2,147,483,647. So if you assign value which is not in this range then compiler would give error.
// compile time error: Cannot implicitly convert type 'long' to 'int'.
int i = 21474836470;
The value of unsigned integers, long, float, double, and decimal type must be suffix by u,l,f,d, and m respectively.
uint ui = 100u;
float fl = 10.2f;
long l = 45755452222222l;
ulong ul = 45755452222222ul;
double d = 11452222.555d;
decimal mon = 1000.15m;
Alias vs .Net Type
The above table of predefined data types are actually alias to their .NET type (CLR class) name. The following table lists alias for predefined data types and related .NET class name.
Alias | .NET Type | Type |
---|---|---|
byte | System.Byte | struct |
sbyte | System.SByte | struct |
int | System.Int32 | struct |
uint | System.UInt32 | struct |
short | System.Int16 | struct |
ushort | System.UInt16 | struct |
long | System.Int64 | struct |
ulong | System.UInt64 | struct |
float | System.Single | struct |
double | System.Double | struct |
char | System.Char | struct |
bool | System.Boolean | struct |
object | System.Object | Class |
string | System.String | Class |
decimal | System.Decimal | struct |
DateTime | System.DateTime | struct |
It means that whether you define a variable of int
or Int32
, both are same.
int i = 345;
Int32 i = 345;// same as above
Default Value
Every data type has a default value. Numeric type is 0, boolean has false and char has '/0'
as default value. Use default(typename)
to assign a default value of the data type or C# 7.1 onward, use default literal.
int i = default(int); // 0
float f = default(float);// 0
decimal d = default(decimal);// 0
bool b = default(bool);// false
char c = default(char);// '\0'
// C# 7.1 onwards
int i = default; // 0
float f = default;// 0
decimal d = default;// 0
bool b = default;// false
char c = default;// '\0'
Conversion
The values of certain data types are automatically converted to the different data types in C#. This is called implicit conversion.
int i = 345;
float f = i;
Console.WriteLine(f); //output: 345
In the above example, value of an integer variable i
is assigned to the variable of float type f
because this conversion operation is predefined in C#.
The following is an implicit data types conversion table.
Implicit Conversion From | To |
---|---|
sbyte | short, int, long, float, double, decimal |
byte | short, ushort, int, uint, long, ulong, float, double, decimal |
short | int, long, float, double, or decimal |
ushort | int, uint, long, ulong, float, double, or decimal |
int | long, float, double, or decimal. |
uint | long, ulong, float, double, or decimal |
long | float, double, or decimal |
ulong | float, double, or decimal |
char | ushort, int, uint, long, ulong, float, double, or decimal |
float | Double |
Conversions from int, uint, long, or ulong to float and from long or ulong to double may cause a loss of precision. No data type implicitly converted to the char type.
However, not all data types are implicitly converted to other data types. For example, int type cannot be converted to uint implicitly, it must be specify explicitly, as shown below.
public static void Main()
{
int i = 100;
uint u = (uint) i;
Console.Write(i);
}
In the above example, integer i
is converted to uint explicitly by specifying uint in the brackets (uint). This will convert an integer to uint.