Introduction
.NET provides a rich set of data types that help you model data efficiently, ensuring type safety and performance. This tutorial walks through the most commonly used data types, their ranges, and practical usage examples.
Primitive Types
| Type | Alias | Size | Range |
|---|---|---|---|
| System.Boolean | bool | 1 byte | true/false |
| System.Byte | byte | 1 byte | 0 – 255 |
| System.SByte | sbyte | 1 byte | -128 – 127 |
| System.Char | char | 2 bytes | Unicode 0 – 65535 |
| System.Int16 | short | 2 bytes | -32,768 – 32,767 |
| System.UInt16 | ushort | 2 bytes | 0 – 65,535 |
| System.Int32 | int | 4 bytes | -2,147,483,648 – 2,147,483,647 |
| System.UInt32 | uint | 4 bytes | 0 – 4,294,967,295 |
| System.Int64 | long | 8 bytes | -9,223,372,036,854,775,808 – 9,223,372,036,854,775,807 |
| System.UInt64 | ulong | 8 bytes | 0 – 18,446,744,073,709,551,615 |
| System.Single | float | 4 bytes | ±1.5 × 10⁻⁴⁵ to ±3.4 × 10³⁸ |
| System.Double | double | 8 bytes | ±5.0 × 10⁻³²⁴ to ±1.7 × 10³⁰⁸ |
| System.Decimal | decimal | 16 bytes | ±1.0 × 10⁻²⁸ to ±7.9 × 10²⁸ |
| System.String | string | Reference | Unicode text |
bool isActive = true;
int age = 30;
double balance = 1234.56;
string message = "Hello, .NET!";
Reference Types
Reference types store references to objects on the managed heap. They include class, interface, delegate, array, and string.
class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
Person p = new Person { Name = "Alice", Age = 28 };
Console.WriteLine(p.Name);
Nullable Types
Value types can be made nullable using the ? suffix, allowing them to represent an undefined state.
int? optionalNumber = null;
if (optionalNumber.HasValue)
Console.WriteLine(optionalNumber.Value);
else
Console.WriteLine("No value");