C# is a programming language where we can define different data types. The data types in C# are the same as in any other language and are used to store a particular set of values in different variables. We store data of one specific type in a variable. Like if we want to store a number, whether it is a decimal number or an integer value, just to classify the values so we use data types. When variables are stored in a memory location, their data types are mentioned with them. Three different data types are categorized: one type stores values, another stores references, and the third points to other locations.
The data type in C# are of three types and two of them are further divided into two. We will discuss all the data types and their categories in detail.
1. Value Data Types
The value Data types are those data types that store the value directly in the memory when a variable is assigned a data type. The memory is reserved for that variable and when we initialize the variable, it stores the value where memory is reserved for that specific variable. The value data types can be signed and unsigned literals. These are of two types as follows:
These are already built-in. We can simply use them with their name.
Signed integral: In signed, the values are in the fixed positive and non-positive numbers range.
The signed integers are:
- Sbyte (Short byte).
- int (integer).
Unsigned integral: In unsigned values are fixed positive ranges.
The unsigned integers are:
- ushort (unsigned short).
- uint (unsigned integer).
- ulong (unsigned long).
Floating point: These values contain decimal point values. There are two different forms of floating point values: float and double. The precision of a float is up to 6 to 7 digits, while the precision of a double is up to 15 digits.
Character: This data type is used to store only one character in a variable and should be surrounded by single quotes.
Boolean: Boolean can only take values as true or false. The ‘bool’ word is used to declare a Boolean-type variable.
These are defined by the users as a data structure or as an enumeration to store different data types in one pla
2. Reference Data Types
This data type does not contain the data but it has the reference of that data. If the actual data changed the reference, the data is also changed. We don’t need to change the data manually. These are of two types as follows:
These are built-in the system so we don’t need to define them: they are already defined. We simply use them to store values. Strings are an example of predefined reference data types.
These are defined by the users like classes. We create a class and then call the methods and variables of classes by creating a reference variable called an object.
3. Pointer Data Types
The pointer data types work as a pointer. They contain the memory address of that variable. These are two, ampersand (&) and asterisk (*). Ampersand (&) refers to the address of that variable and asterisk (*) gives us the value of an address.
In this scenario, we will find the size of all the data types by using the sizeof() method.
In the first statement, display a message on the screen and concatenate that with the data type whose size we want to find. The sizeof() method is utilized to get the size of all the required data types. We mention the specific data type inside this function. The first data type is an integer, it will display the size of the integer and size is the space that this data type takes in the memory. The integer takes 4 bytes of memory to store one integer value. Every data type takes different bytes, if integers take 4 bytes to store one variable, then double takes 8. The minimum byte is 1. Here, we will acquire the size of all the data types that are already built into the system. String does not have a specific size, it varies. Its size is determined by how long the string is. Repeat the same code line multiple times to find the size of all the data types. We just change the names of data types in that code line.
The table of data types is displayed on the console. It shows that only bool, byte and sbyte take 1 byte of memory to store one variable.
In this instance, two variables having distinct data types are added. The result is stored in a variable of a different type.
First, we declared and initialized two variables. One is of ‘integer’ type and stores an integer value in it. The second is a ‘floating point’ data type and it stores a floating point value in it. Declare another variable of type ‘double’. In this variable, we will store the resultant value. Both the first two variables belong to different data types. If we add these variables then typecasting will be used. So before writing a variable, specify the data type in which we want to change that variable. This typecasting will convert the variable to that mentioned data type. We have done the typecasting of a float value. Then after addition, we also changed the data type of the answer by type casting. If we don’t change the data type of the outcome, we won’t be able to save the resultant value in the ‘double’ variable. Now, display the message along with the result by calling the Console.Write() function.
We have discussed the data types in C#. There are three main categories of data types and they perform different functionalities like ‘int’ to store integer type values, ‘float’ to store floating point values, and so on. Without data types, it is impossible to do even coding because without them we could not store values in the memory location. Without them, we could still store values, but it would be immensely difficult to manipulate those values. The data types make computation easy, as they categorize the types of data so we can perform different operations on the variables. We can also add, and subtract two variables of different data types by doing their typecasting.