C# is a strongly typed, object-oriented programming language. Therefore, data types determine the characteristics and behavior of the variables manipulated throughout the execution of a program.
In C#, types are divided into two main categories:
- Value types, which contain their data directly
- Reference types, which contain a reference to their data
If you want to learn more about Data Types
check out the Introduction to Programming Course read more
Value Types
C# offers a series of predefined value types that correspond to the primitive types of the .NET Framework. Some of the most common are:
Integers
The integer data types in C# are used to represent whole numbers (without decimal part). Some of the most common integer data types are:
Type | Bytes | Signed/Unsigned | Minimum Value | Maximum Value |
---|---|---|---|---|
byte | 1 | Unsigned | 0 | 255 |
sbyte | 1 | Signed | -128 | 127 |
short | 2 | Signed | -32,768 | 32,767 |
ushort | 2 | Unsigned | 0 | 65,535 |
int | 4 | Signed | -2,147,483,648 | 2,147,483,647 |
uint | 4 | Unsigned | 0 | 4,294,967,295 |
long | 8 | Signed | -9,223,372,036,854,775,808 | 9,223,372,036,854,775,807 |
ulong | 8 | Unsigned | 0 | 18,446,744,073,709,551,615 |
Examples,
int number = 10;
long longNumber = 100000L; // L is necessary to indicate that it's a long type
short shortNumber = 30000;
Floating Point Numbers
The floating point data types in C# are used to represent numbers with a decimal part. Some of the most common floating point data types are:
Type | Bytes | Precision | Minimum Value | Maximum Value |
---|---|---|---|---|
float | 4 | Single precision | ±1.5 x 10^-45 | ±3.4 x 10^38 |
double | 8 | Double precision | ±5.0 x 10^-324 | ±1.7 x 10^308 |
decimal | 16 | High precision | ±1.0 x 10^-28 | ±7.9 x 10^28 |
Examples:
float floatNumber = 10.5f; // f is necessary to indicate that it's a float type
double doubleNumber = 20.99;
decimal decimalNumber = 100.50m; // m is necessary to indicate that it's a decimal type
Boolean Data Types
The boolean data type in C# is used to represent values of true
or false
. They are necessary when we need to evaluate conditionals.
Example:
bool isTrue = true;
bool isFalse = false;
Character Data Types
The character data type in C# is used to represent a single character (a letter). The char data type is used to store Unicode characters.
For example, it can be used to store letters, numbers, or special characters.
char letter = 'A';
In C#, characters are delimited with single quotes '
.
Structures
struct
allows the creation of user-defined value grouping types.
public struct Point
{
public int X;
public int Y;
}
Point point = new Point();
point.X = 10;
point.Y = 20;
Enumerations
enum
allows the creation of a set of related constants under a specific type name.
Example:
enum Days
{
Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
}
Days today = Days.Monday;
Reference Types
Reference types include string
, arrays
, classes (class
), and delegates. For example, string
is a class provided by C# to work with text strings.
Example of using a reference variable, in this case a string
.
int[] numbers = {1, 2, 3, 4, 5};
string greeting = "Hello, World!";
We will see these types in more detail in later entries