C# is an object-oriented, strongly typed programming language. Therefore, data types determine the characteristics and behavior of the data manipulated throughout the execution of a program.
In C#, types are divided into two major categories:
- Value types, directly contain their data and are stored on the memory stack
- Reference types, contain a reference to their data, which is stored on the heap
Value types
C# offers a series of predefined value types that correspond to the primitive types in the .NET Framework. Some of the most common integer data types are:
Integers
Integer data types in C# are used to represent whole numbers without a 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 |
Example:
int number = 10;
long longNumber = 100000L; // L is necessary to indicate it is a long type
short shortNumber = 30000;
Floating-point numbers
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 it is a float type
double doubleNumber = 20.99;
decimal decimalNumber = 100.50m; // m is necessary to indicate it is a decimal type
Boolean data types
The boolean data type in C# is used to represent true
or false
values. They are necessary when evaluating conditionals.
Example:
bool isTrue = true;
bool isFalse = false;
Character data types
The character data type in C# is used to represent a single character. The char data type is used to store Unicode characters. For example, it can be used to store letters, numbers, or special characters.
In C#, characters are delimited with single quotes '
.
char letter = 'A';
Structures
structs
allow 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
enums
allow 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, the string
type is a class provided by C# for working 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 look at these types in more detail in future entries.