C# Data Types are used to define a type of data that can behold by variable. Data Types such as integer, float, string, etc., are used in our application.
Language which is strongly typed such as C# programming language, it’s mandatory to define a variable with the data type to indicate what type of data, variable can hold, before performing any operation on Variable.
Defining C# Data Types:
Following is the Syntax used for defining C# data types.
In above-giving syntax [Data type] shows the type of data to be held in a variable. [Variable Name] shows the name of the variable to be used in the application. [Value] is used to show the required value to the variable.
Data Types in C#:
There are 3 main types of data in C#.
- Value Data Types
- Pointer Data Type
- Reference Data Types
1- Value Data Types:
In C# the Value Data Types is used to store the variable value directly in memory. It can accept both signed and unsigned literals. Value Data Types are derived from the class System.ValueType.
Given below are the different Value Data Types used in C#.
Type | Represents | Range | Default Value |
byte | Byte | 8 bits | 0 to 255 |
sbyte | SByte | 8 bits | -128 to 127 |
bool | Boolean | 8 bits | True or False |
short | Int16 | 16 bits | -32,768 to 32,767 |
ushort | UInt16 | 16 bits | 0 to 65,535 |
int | Int32 | 32 bits | -2,147,483,648 to 2,147,483,647 |
uint | UInt32 | 32 bits | 0 to 4294967295 |
float | Single | 32 bits | -3.402823e38 to 3.402823e38 |
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 |
double | Double | 64 bits | -1.79769313486232e308 to 1.79769313486232e308 |
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 |
2- Pointer Data Type:
In C#, the Pointer Data Types are used to have a memory address of the variable value, having the same capabilities as the pointers in C or C++.
There are 2 Symbols to get the pointer details,
ampersand (&) known as the Address Operator, which is used to determine the address of a variable.
asterisk (*) known as the Indirection Operator, which is used to access the value of an address.
Given below is the Syntax of defining the pointer type in the C# data types.
3- Reference Data Types:
In C# data types, Reference Data Types is used to have a memory address of the variable value. It won’t store the variable value directly in memory. Basically, It refers to a memory location.
There are some built-in reference types. Given below,
Dynamic: Used to store any type of value in the dynamic data type variable. For dynamic data types of variables, Type checking takes place at run-time.
Given below is the Syntax for declaring a dynamic type in C# data types:
String: Represents a sequence of Unicode characters and its type name is System.String. Considering that Variable, value, and Value are equivalent.
Object: It is considered as the base class for all the data types in C#. Type conversion is needed, before assigning values.
When a variable of a value type is converted to object, it’s called boxing.
When a variable of type object is converted to a value type, it’s called unboxing. Its type name is System.Object.