C#  

Learn Data Types in C#

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:

  • int is the data type

  • It defines that age can store integer values

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:

  1. Value Types

  2. Reference Types

  3. 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

TypeSizeRange
byte1 byte0 to 255
sbyte1 byte-128 to 127
short2 bytes-32,768 to 32,767
ushort2 bytes0 to 65,535
int4 bytes-2,147,483,648 to 2,147,483,647
uint4 bytes0 to 4,294,967,295
long8 bytesVery large negative to positive numbers
ulong8 bytesOnly positive large values

Example

int count = 10;
long population = 7800000000;

B. Floating Point Types

TypeSizeUse Case
float4 bytesGood for scientific calculations (less precision)
double8 bytesDefault for decimal operations (more precision)
decimal16 bytesBest 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:

  • Low-level programming

  • Interoperability

  • Hardware-related operations

How Data Types Are Stored in Memory

Stack Memory

  • Stores value types

  • Fast access

  • Automatically cleaned

Heap Memory

  • Stores reference types

  • The garbage collector manages memory

  • Used for large or dynamic data

Reference Types Store

  • Object data in the heap

  • Address (reference) in stack

Choosing the Right Data Type

  1. Use int for counting and common whole numbers.

  2. Use long for very large numbers.

  3. Use float/double for scientific calculations.

  4. Use decimal for financial values.

  5. Use bool for true/false conditions.

  6. Use a string for text, but avoid unnecessary concatenations.

  7. Use var only when the type is obvious.

  8. Use record for immutable models.

  9. Avoid dynamic unless necessary.

  10. Use an enum for fixed sets of values like statuses.