A value or a set of values is data for a computer. There are many types of Data (Value or a Set of Values) even in the real world. For example, we show the age of a person as a number while defining that person’s name as a set of characters. Based on this concept, different types of data types have been defined to store different types of data in the C# language also.
I noticed, in reality, there are only two types of data. Either the data is numerical, which contains only numerical values , and depending on the need, a specific type of calculation can be performed with them, or it is alphanumerical, which is a group of characters, with which any type of calculation can be performed. It cannot be performed.
C# is a Strongly Typed Programming Language. That is, in this programming language, before storing any type of value in the computer’s memory, the memory is reserved for storing that particular type of value, and the compiler is told that the memory we are reserving, in that What kind of value will you store?
As a result, during the execution of the entire program, the C# compiler takes care that the memory location, which is defined to store the type of data, stores the same type of value at that memory location. While trying to store another type of value generates a compiler error.
Data Types in C#: Value Types and Reference Type
Basically, different types of built-in data types in C# are divided into two parts named Value Types and Reference Types. The difference between these two types is based on what type of value is stored in a variable.
If there is any Actual Numerical or Non-Numerical Data Stored in a Variable, then this type of variable is called Variable of Value Type. Whereas if there is no data stored in the variable, but the address of the memory location on which the data is actually stored, then this type of variable is called Reference Type Variable.
We can also call Value Type in simple words as Primary Data Type, whereas Reference Types are actually Secondary Data Type or User-Defined Data Type, which a programmer himself defines according to his need. There are basically 13 value types in C# Core, which are shown in the following table:
|bool||Represents true/false values|
|byte||8-bit unsigned integer|
|decimal||Numeric type for financial calculations|
|double||Double-precision floating point|
|float||Single-precision floating point|
|sbyte||8-bit signed integer|
|uint||An unsigned integer|
|ulong||An unsigned long integer|
|ushort||An unsigned short integer|
These Value Data Types form the basis of C#’s Type System, as all other User Defined Data Types are actually based on them. That is why they are also commonly called Primary Data Type or Primitive Types.
Each Value Type in C# has a fixed Range and Behavior, due to which all .NET Supported Programming Languages provide the facility of Language Interoperability by accessing and manipulating each other’s codes.
Apart from these Simple Value Types, Value Types in C# have been sub-divided into three more categories named Enumerations, Structures, and Nullable Types, which we will learn about in further detail.
Primitive Data Types are Standard Data Types, while Secondary or Reference Types are based on Primitive Data Types. We can divide the different data types of C# as follows:
Data Type is a fundamental concept of any programming language. Using Data Type, a programmer tells the compiler what type of value it has to store. The compiler does two things based on the data type decided by the programmer.
- The compiler reserves the memory according to the data type.
- Gives a name to that Reserved Memory.
Just as in the real world, less space is needed to keep small things, and more space is needed to store big things, in the same way, it happens in computers too. If the small value is to be stored in a computer, then less memory is required, and if a large value is to be stored, then more memory is required.
Let us try to understand this fact with an example. We know that a byte of memory is needed to store a character in a computer.
That is, if the name of a student is ten characters, then 10 Bytes will be required to store that student’s name in memory, whereas if the name of a student is 15 characters, then 15 Bytes of memory will be required.
Similarly, if the age of a student is to be stored in the computer, then we will need less memory. Whereas if we want to store the total population of a country as a value in a computer, then we will need much more memory than the memory taken by the age to store the age because practically the population of any country is less than Even if it is less, it will be in crores, whereas if the maximum age of a person was, it would be in hundreds only.
That is if we say in summary, different memory size is needed to store the values of different types in memory and we have to store and manipulate what kind of value related to any real-world problem. Depending on this, the appropriate type of data type has to be declared as Variable or Constant.
Variable or Constant Declaration
When we have to do any kind of processing on any data in the computer, then, first of all, we have to store that data in the memory of the computer. Different types of keywords have been fixed in C# to store any data in the computer’s memory. By these keywords, the compiler decides what type of memory the user wants to store.
The data type of which the user uses the keyword, the compiler reserves the memory according to the size of that keyword. But there is still a problem, and that problem is that the compiler reserves the memory to send the values (data) to that memory, and to get the data back from those memory locations in some processing, the programmer has to give a name. It is needed so that it can tell the compiler which memory location value it wants to access.
Therefore, the programmer has to give a name along with the keyword representing the data type. This name itself is called Variable or Constant.
To store a value in the computer, the compiler has to tell two things. The first is what kind of value the programmer wants to store in memory. To tell this first thing, the programmer uses a keyword as per the need. Based on this keyword, the compiler reserves the memory.
Whereas the compiler has to tell the second thing that by what name the programmer will access that reserved memory, and to fulfill this need, the programmer also specifies a name with the keyword of the data type and the compiler that is reserved. He associates the memory with the specified name.
When a programmer gives information to the compiler that what type of value he wants to store in memory and by what name he wants to access the memory to be reserved by the compiler, then this whole process is called Variable / Constant Declaration. . To declare a Variable / Constant, the programmer writes his code in the source file of C# according to the following syntax-
If we want to create or declare more than one identifier of the same data type by the same syntax at the same time, then we have to use the following syntax-
DataType Identifier_Name_1, Identifier_Name_2, , . . . , Identifier_Name_n,
The method of declaring both Variable and Constant identifiers is the same. The difference is only in the way the data is stored in them.
When a certain value is assigned to an identifier as soon as it is declared and the programmer does not want to change that value in the whole program, then that identifier declared is called Constant. Whereas if after declaring the identifier, the programmer does not initialize a permanent value in it, then that identifier is called a variable.
When the programmer declares an identifier and provides an initial value in it along with the declaration, then this process is called Variable Initialization. Generally, identifiers are initialized in almost all languages.
There are also some high-level languages in which even if the identifier is not initialized as soon as it is declared, the compiler of those languages initializes the default value in those identifiers. C# is also one such Smart High-Level Programming Language. Therefore, even if we do not initialize any value by creating any of its identifiers, C# itself initializes its variables according to their data type.