.NET Data Types

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

TypeAliasSizeRange
System.Booleanbool1 bytetrue/false
System.Bytebyte1 byte0 – 255
System.SBytesbyte1 byte-128 – 127
System.Charchar2 bytesUnicode 0 – 65535
System.Int16short2 bytes-32,768 – 32,767
System.UInt16ushort2 bytes0 – 65,535
System.Int32int4 bytes-2,147,483,648 – 2,147,483,647
System.UInt32uint4 bytes0 – 4,294,967,295
System.Int64long8 bytes-9,223,372,036,854,775,808 – 9,223,372,036,854,775,807
System.UInt64ulong8 bytes0 – 18,446,744,073,709,551,615
System.Singlefloat4 bytes±1.5 × 10⁻⁴⁵ to ±3.4 × 10³⁸
System.Doubledouble8 bytes±5.0 × 10⁻³²⁴ to ±1.7 × 10³⁰⁸
System.Decimaldecimal16 bytes±1.0 × 10⁻²⁸ to ±7.9 × 10²⁸
System.StringstringReferenceUnicode 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");