Introduction
C# is a strongly-typed programming language. It means that we should declare the data type of any variable that indicates the type and range of values it will store, such as float, integer, char, decimal, text, etc.
This article will focus on different types of Data types in C#, their default values, and ranges.
Recommended Topic, Palindrome in C# and Ienumerable vs Iqueryable.
Data Types in C#
Data types in C# specify the type of data that any variable can store, such as float, integer, char, etc.
Data types in C# are mainly divided into three categories−
- Value Data Types
- Reference Data Types
- Pointer Data Types
Value Data Types
In C#, the value data types will store the variable value in the memory. The value data types are floating-point-based and integer-based. C# supports both unsigned and signed literals.
We have mainly two types of value Datatypes in C#:
- Predefined Data Types - Char, Integer, Float, Boolean, etc.
- User-defined Data Types - such as Enumerations, Structure, etc.
Now let's see some of the value data types in the below table:
Data types |
Memory size |
Range |
char | 1 byte | -128 to 127 |
unsigned char | 1 byte | 0 to 127 |
signed char | 1 byte | -128 to 127 |
short | 2 byte | -32768 to 32767 |
unsigned short | 2 byte | 0 to 65535 |
signed short | 2 byte | -32768 to 32767 |
int | 4 byte | -2147483648 to 2147483647 |
unsigned int | 4 byte | 0 to 4294967295 |
signed int | 4 byte | -2147,483,648 to 2147,483,647 |
long | 8 byte | -9,223,372,036,854,775808 to 9,223,372,036,854,775807 |
unsigned long | 8 byte | 0 to 18,446,744,073,709,551615 |
signed long | 8 byte | -9,223,372,036,854,775808 to 9,223,372,036,854,775807 |
float | 4 byte | 1.5*10-45 to 3.4 *1038, with 7 digit precision |
double | 8 byte | 5.0*10-324 - 1.7*10308, with 15 digit precision |
decimal | 16 byte | at least -7.9*10?28 - 7.9*1028, with at least 28-digit precision |
For example:
int myNum = 10; // Integer (whole number)
double myDoubleNum = 10.99; // Floating point number
char myLetter = 'C'; // Character
bool myBool = false; // Boolean
The above code shows an example of value data types.
Reference Data Types
The reference data types do not contain any actual data stored in the variable, but they contain the variables' reference or memory address. If the data is changed by any of the reference data type variables, the other variable will automatically reflect the change in value.
We may categorize reference Data types in C# into two parts:
- Predefined Types: These are built-in reference data types. For example, Objects and Strings.
- User-defined Types: These are user-defined reference types. For example, Classes, Interface.
For example:
string myText = "CodingNinjas"; // String
The above code shows an example of predefined reference data types.
Pointer Data Types
Pointers in the C# programming language are variables; it is also known as an indicator or locator that points to the address of a value.
Pointer Data Type
In the above diagram, we can see that a pointer variable contains the address of any other variable.
Symbols used to represent pointers in data types in C#
We use different symbols or characters to represent pointer Datatypes in C#. These symbols are listed below in the table with their name and description.
Symbol | Name | Description |
& ( ampersand sign ) | Address operator | We use it to determine the address of any variable. |
* ( asterisk sign ) | Indirection operator | We use it to access the value of any address. |
Declaring a pointer
We can declare a pointer in C# using the asterisk symbol ( * ). Let's see an example of declaring a pointer in C#.
int* p1, p; // Valid syntax
int *p1, *p; // Invalid