Data types are everywhere in programming. They are used to define the type of variable declared in a program. In C#, there are three types of data types:
- The value types.
- The reference type (Object, dynamic, and string type)
- Pointer data type. We will discuss how to define, assign, and manipulate variables in the next chapter.
The value type is the variable that directly contains the data. Whenever you declare a variable of type int, float, char, you are creating a value data type variable. In .NET Framework, value data types are derived from the class System.ValueType.
The table below represents the built-in value type in C# in alphabetical order:
|bool||bool or Boolean hold of two values, true or false.||1 byte|
|byte||Holds 8-bit unsigned integers. Can only hold positive numbers.||8 bit||0 to 255|
|char||Holds 16-bit Unicode characters.||16 bit||0 to 65,535|
|decimal||Holds a 128-bit signed floating-point value with 28 digit precision.||128 bit||–7.9 × 10^−28 to 7.9 × 10^28|
|double||Holds a 64-bit signed floating-point value with 15 digit precision,||64 bit||5.0 × 10^−324 to 1.7 × 10^308|
|float||Holds a 32-bit signed floating-point value with 7 digit precision.||32 bit||1.5 × 10^−45 to 3.4 × 10^38|
|int||Holds 32-bit signed integers.||32 bit||-2,147,483,648 to 2,147,483,647|
|long||Holds 64-bit signed integers.||64 bit||–9,223,372,036,854,775,808 to 9,223,372,036,854,775,807|
|sbyte||Holds 8-bit signed integers.||8 bit||-128 to 127|
|short||Holds 16-bit signed integers.||16 bit||-32,768 to 32,767|
|uint||Holds 32-bit unsigned integers.||32 bit||0 to 4,294,967,295|
|ulong||Holds 64-bit unsigned integers.||64 bit||0 to 18,446,744,073,709,551,615|
|ushort||Holds 16-bit unsigned integers.||16 bit||0 to 65,535|
Unlike value type, reference types do not contain the actual data assigned to a variable, but they contain a reference to the variable. The reference type points to a memory location where the value of a variable is stored.
The tree reference types in C# are: object, dynamic, and string.
The object data type is the base class of all types in C#. That means object data types can hold all sort of values. An object data type can be converted to value type, and vice versa. These operations are called boxing and unboxing. We will explain in other chapter in detail about the boxing and unboxing operations.
A dynamic variable can have any type, which changes during the runtime. the dynamic type is similar for the object data type, e except of that the type checking for object take place in the compile time, while the type checking of dynamic take place in the runtime. The dynamic data type usage comes with a complication and it’s advised to avoid using it.
The string type allows you to assign encoding characters values to a variable. It’s very important to know how to deal with string data type, since string is immutable, or ready only. Once a string is assigned a value, the content can’t be changed. So, when you try to manipulate a string, the value doesn’t change, but instead a new reference of that variable will get created in the memory containing the new value. This is why string is not a good for large data manipulation, such as creating a dynamic SQL Query or an error log message. That is why we use the string builder instead, which is derived from the System.Text Class. We will discuss in details about string builder in later chapter.
The pointer types in C# are similar to the ones in C and C++. However, it’s rarely used in C#. More details will be discussed on pointer type later in this tutorial.