A data type defines the kind of data a variable can store—such as numbers, characters, text, true/false values, decimal values, and more.
Understanding data types is extremely important because:
They determine memory allocation
They define a range of values
They ensure type safety, a powerful feature of C#
They help detect errors at compile time
They improve program reliability and performance
In this article, we explore all major data types in C#, their categories, memory size, usage, examples, and important notes you must know as a .NET developer.
What Are Data Types in C#?
A data type specifies:
What kind of data a variable can hold
How much memory does it need
What operations are allowed
Example
int age = 25;
Here:
C# is a strongly typed language, meaning every variable must have a clearly defined data type.
Categories of Data Types in C#
C# has three main categories:
Value Types
Reference Types
Pointer Types (unsafe code only)
We will explore each in detail.
Value Types (Stored in Stack Memory)
Value types hold the actual value in the memory location. They are stored in stack memory, which is fast and automatically managed.
Value types include:
Numeric types
Boolean
Character
Structs
Enums
Let’s look at all value types.
A. Numeric Integral Types
| Type | Size | Range |
|---|
| byte | 1 byte | 0 to 255 |
| sbyte | 1 byte | -128 to 127 |
| short | 2 bytes | -32,768 to 32,767 |
| ushort | 2 bytes | 0 to 65,535 |
| int | 4 bytes | -2,147,483,648 to 2,147,483,647 |
| uint | 4 bytes | 0 to 4,294,967,295 |
| long | 8 bytes | Very large negative to positive numbers |
| ulong | 8 bytes | Only positive large values |
Example
int count = 10;
long population = 7800000000;
B. Floating Point Types
| Type | Size | Use Case |
|---|
| float | 4 bytes | Good for scientific calculations (less precision) |
| double | 8 bytes | Default for decimal operations (more precision) |
| decimal | 16 bytes | Best for financial and money calculations |
Example
float height = 5.9f;
double weight = 65.567;
decimal price = 199.99m;
Note
float — suffix f
decimal — suffix m
C. Boolean Type
bool isActive = true;
Represents true/false values.
D. Character Type
char grade = 'A';
Stores a single Unicode character.
E. Struct Type
Structs are value types used for small, lightweight objects.
public struct Point
{
public int X;
public int Y;
}
F. Enum Type
An enum defines a set of named constants.
public enum OrderStatus
{
Pending,
Shipped,
Delivered,
Cancelled
}
Reference Types (Stored in Heap Memory)
Reference types store the reference (address) to the actual data, which is stored in heap memory.
Reference types include:
Classes
Interfaces
Arrays
Strings
Delegates
Records (C# 9+)
Let’s explore these.
A. Class Type
A class is a blueprint for objects.
public class Person
{
public string Name;
public int Age;
}
Example usage
Person p = new Person();
p.Name = "Ajay";
B. Array Type
Arrays hold a collection of values of the same type.
int[] numbers = { 1, 2, 3, 4 };
Arrays live in the heap regardless of element type.
C. String Type
Strings are reference types but behave like value types because they are immutable.
string message = "Hello World";
D. Interface Type
An interface defines a contract but has no implementation.
public interface IAnimal
{
void Speak();
}
E. Delegate Type
Delegates store references to methods.
public delegate void PrintMessage(string msg);
F. Records (Modern C#)
Introduced in C# 9, records are reference types meant for immutable data modeling.
public record User(int Id, string Name);
Nullable Types
Nullable types allow value types to store null.
int? age = null;
Useful when representing:
Optional fields
Database values
Missing data
Implicitly Typed Variables (var)
var total = 50; // int
var name = "Ajay"; // string
The compiler decides the type based on the assigned value.
Dynamic Type
Dynamic bypasses compile-time type checking.
dynamic value = 10;
value = "Now string";
Useful when working with:
COM objects
JSON
Reflection
Object Type – The Base of All Types
object is the root type of all types in C#.
object data = 10;
data = "Hello"; // allowed
All classes inherit from object.
Pointer Types (Unsafe Code Only)
Pointer types allow working with memory addresses directly.
unsafe
{
int num = 10;
int* p = #
}
Used mainly in:
How Data Types Are Stored in Memory
Stack Memory
Stores value types
Fast access
Automatically cleaned
Heap Memory
Reference Types Store
Choosing the Right Data Type
Use int for counting and common whole numbers.
Use long for very large numbers.
Use float/double for scientific calculations.
Use decimal for financial values.
Use bool for true/false conditions.
Use a string for text, but avoid unnecessary concatenations.
Use var only when the type is obvious.
Use record for immutable models.
Avoid dynamic unless necessary.
Use an enum for fixed sets of values like statuses.