As you have already known that while declaring a variable in C#, you have to specify the data type with the variable name. This is how you tell the compiler what type of data the variable will hold. Almost all programming language needs the concept of the data type. Since C# is a strongly typed language, it is essential to inform the compiler what kind of data these memory locations will hold. In this chapter, you will learn about the different data types that are supported by C#.
Data Types in C#
- Value Type
- Reference Type
- Pointer Type
Let us discuss each of the types in details:
When a variable can be assigned a value directly, they are termed as Value Type variables. Their origination comes from the System.ValueType class. They are the preliminary data types of the C# language.
As soon as a programmer declares a variable with a data type, the compiler allocates some memory (based on the number of bytes it takes) for storing the value.
C# allows you to define eight different types of integral variables. They provide support for 8-bit, 16-bit, 32-bit and 64-bit values with signed & unsigned modifiers. These eight types are mentioned below with their keyword, range, class name and default value.
|ALIAS||TYPE||TYPE NAME||SIZE(BITS)||RANGE||DEFAULT VALUE|
|short||signed integer||System.Int16||16||-32768 to 32767||0|
|sbyte||signed integer||System.Sbyte||8||-128 to 127||0|
|Int||signed integer||System.Int32||32||-231 to 231-1||0|
|long||signed integer||System.Int64||64||-263 to 263-1||0L|
|byte||unsigned integer||System.byte||8||0 to 255||0|
|ushort||unsigned integer||System.UInt16||16||0 to 65535||0|
|uint||unsigned integer||System.UInt32||32||0 to 232||0|
|ulong||unsigned integer||System.UInt64||64||0 to 263||0|
Floating Point Type
Floating point data types in C# are of two types. These are:
- 32-bit single (7-digit) precision floating point type declared using the keyword float. For initializing any variable with a float, you have to mention a 'f' or 'F' after the value. For example: float g = 62.4f; In case you do not use the suffix, then compiler treats the value as double.
- 64-bit (14-15 digit) precision floating point type declared using the keyword double. For initializing any variable with double, you have to mention a 'd' or 'D' after the value. For example, double ks =8.403122d;
There is another type of variable that is used for extensive calculations which are of 128-bit used for calculating huge economic data. It uses' or 'M' as the suffix; otherwise, the value will be treated as double.
This is used for representing 16-bit Unicode character used for storing a single character.
A reference type is another form of the variable that does not hold the actual value or data, instead, it possesses a reference to any memory location that is assigned with a variable. This refers to a memory location
The built-in reference types provided by C# are:
All these will be covered in the later chapter of this tutorial.
These types of variables are used for storing the address of any memory location which is of another type. Pointers are considered a separate data type kind because they do not hold the actual value or data; rather they are meant to store the actual memory location. The concept of pointers came in C# from C and C++.
The syntax of declaring pointers in C# is:
char* str_name; int* user_id;