The C# language comes with a set of Basic data types. These data types are used to build values which are used within an application. Let’s explore the basic data types available in C#.
Data types specify the type of data that a valid C# variable can hold. C# is a strongly typed programming language because in C#, each type of data (such as integer, character, float, and so forth) is predefined as part of the programming language and all constants or variables defined for a given program must be described with one of the data types.
The CSharp type system contains three Type categories. They are Value Types, Reference Types and Pointer Types.
The Value Types store the data while the Reference Types store references to the actual data. Pointer Types variable use only in unsafe mode. The Value Types derived from System.ValueType
and the Reference Types derived from System.Object
.
The main difference between Value Types and Reference Types is that how these Types store the values in memory. Common Language Runtime (CLR) allocates memory in Stack and the Heap . A Value Type holds its actual value in memory allocated on the Stack and Reference Types referred to as objects, store references to the actual data. In C# it is possible to convert a value of one type into a value of another type . The operation of Converting a Value Type to a Reference Type is called Boxing and the reverse operation is called Unboxing.
Data types in C# is mainly divided into three categories:
The value data types are integer-based and floating-point based. C# language supports both signed and unsigned literals.
In C#, the Value Data Types will directly store the variable value in memory and it will also accept both signed and unsigned literals. The derived class for these data types are System.ValueType
.
There are 2 types of value data type in C# language:
The memory size of data types may change according to 32 or 64 bit operating system.
The following table lists the value data types in c# programming language with memory size and range of values.
Data Type | .NET Type | Size | Range |
---|---|---|---|
byte | Byte | 8 bits | 0 to 255 |
sbyte | SByte | 8 bits | -128 to 127 |
int | Int32 | 32 bits | -2,147,483,648 to 2,147,483,647 |
uint | UInt32 | 32 bits | 0 to 4294967295 |
short | Int16 | 16 bits | -32,768 to 32,767 |
ushort | UInt16 | 16 bits | 0 to 65,535 |
long | Int64 | 64 bits | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
ulong | UInt64 | 64 bits | 0 to 18,446,744,073,709,551,615 |
float | Single | 32 bits | -3.402823e38 to 3.402823e38 |
double | Double | 64 bits | -1.79769313486232e308 to 1.79769313486232e308 |
bool | Boolean | 8 bits | True or False |
decimal | Decimal | 128 bits | (+ or -)1.0 x 10e-28 to 7.9 x 10e28 |
DateTime | DateTime | - | 0:00:00am 1/1/01 to 11:59:59pm 12/31/9999 |
The reference data types do not contain the actual data stored in a variable, but they contain a reference to the variables.
If the data is changed by one of the variables, the other variable automatically reflects this change in value.
There are 2 types of reference data type in C# language:
The following table lists the reference data types in c# programming language with memory size and range of values:
Data Type | .NET Type | Size | Range |
string | String | Variable Length | 0 to 2 billion Unicode characters |
object | Object | - | - |
The pointer in C# language is a variable, it is also known as locator or indicator that points to an address of a value.
To get the pointer details we have a two symbols ampersand (&
) and asterisk (*
):
&
): It is Known as Address Operator. It is used to determine the address of a variable.*
): It also known as Indirection Operator. It is used to access the value of an address.Following is the syntax of declaring the pointer type in the c# programming language:
type* identifier;
Following is the example of defining the pointer type in the c# programming language:
int* a;
int* b;
Example:
int* p1, p; // Valid syntax
int *p1, *p; // Invalid syntax
The following table lists the detail of different type pointer symbols available in the c# programming language:
Symbol | Name | Description |
& | Address Operator | Determine the address of a variable. |
* | Indirection Operator | Access the value of an address. |
The pointer in C# language can be declared using * (asterisk symbol).
int * a; //pointer to int
char * c; //pointer to char