![]() |
VOOZH | about |
Data types specify the type of data that a variable can store. Whenever a variable is declared in C#, the compiler allocates memory based on the data type.
Below is an example of integer data type.
10
In the above code, the keyword int specifies that the variable will store integer type data.
C# data types are mainly divided into three categories:
These store the actual value directly in memory. The value is stored in the variable itself, so each variable has its own copy of the data. Changes made to one variable do not affect another variable.
int: Used to store integer (whole number) values. It is one of the most commonly used data types in C#. It typically stores values within the range of -2³¹ to 2³¹ - 1.
10
float: Used to store decimal numbers with single precision. It requires the suffix f or F during initialization. It is useful when memory usage needs to be optimized.
3.5
double: Used to store decimal numbers with higher precision than float. It is the default type for decimal values in C#. It provides better accuracy for calculations.
5.6
char: Used to store a single Unicode character. The value is enclosed in single quotes (' '). It occupies 2 bytes in memory.
A
bool: Used to store logical values such as true or false. It is mainly used in conditional statements. It helps in decision-making within programs.
True
These store the reference (memory address) of the data instead of the actual value. Multiple variables can refer to the same data in memory. Any changes made through one variable will affect the other variables referring to the same object.
string: Used to store a sequence of characters (text). It is a reference data type in C#. Strings are immutable, meaning their value cannot be changed after creation.
Hello
object: The base type of all data types in C#. It can store values of any type using boxing. It provides flexibility when working with different data types.
10
array: Used to store multiple values of the same data type. Elements are stored in contiguous memory locations. It allows efficient access using index.
1
These store the memory address of variables instead of actual values. They are mainly used in advanced scenarios such as low-level memory manipulation.
pointer (int* p): Used to store the memory address of a variable.
Note: Pointer types require the use of
unsafecode to access and modify memory directly.