C# Data Types

In the previous chapter, we understood what variables are and how they are used to store data.

Now the next important question is — what kind of data can a variable store?

This is where data types in C# come into the picture.

Without data types, the system would not know how much memory to allocate, what operations are allowed, or how to interpret the stored value.


What are Data Types in C#?

In C#, a data type defines the type of value a variable can hold.

It also determines:

  • How much memory will be allocated
  • What operations can be performed
  • How the value is stored internally

Let’s start with a simple example:

int num = 7;               
double dbl = 7.77;  
char letter = 'C';         
bool isActive = true;          
string text = "Hello";     

Each variable stores a different type of data — and that is defined using data types.


Why Data Types Matter (Real Understanding)

Let’s understand this with a real-world scenario.

Imagine you are building a banking application.

  • Account balance → needs decimal precision → double or decimal
  • Customer name → text → string
  • Account active status → yes/no → bool
  • Number of transactions → whole numbers → int

If you use the wrong data type:

  • You may lose precision
  • You may waste memory
  • You may introduce bugs

So choosing the correct data type is not just coding — it is design thinking.


Default Values of Data Types in C#

In C#, every data type has a default value that is automatically assigned when a variable is created but not explicitly initialized.

This behavior is especially important when working with fields, arrays, and objects.

Why Default Values Matter

Let’s understand this with a simple scenario.

Imagine you are building a user profile system, and you forgot to assign values to some fields.

Instead of crashing, C# assigns default values to ensure stability.

This helps prevent unexpected errors during runtime.

Default Values of data types

Data Type Default Value
int 0
double 0.0
float 0.0f
bool false
char '\0' (null character)
string null
reference types null

Example

class User
{
    int age;
    bool isActive;
    string name;
}

class Program
{
    static void Main()
    {
        User user = new User();

        Console.WriteLine(user.age);       // 0
        Console.WriteLine(user.isActive);  // false
        Console.WriteLine(user.name);      // null
    }
}

Even though we never assigned values, C# automatically initializes them.


Important Note

Local variables DO NOT get default values.

You must initialize them before use, otherwise you will get a compile-time error.

int x;

Console.WriteLine(x); // Error: Use of unassigned variable

Real-World Insight

Default values help avoid application crashes, but relying on them blindly can cause logical bugs.

For example, a user's age being 0 might not be valid — it may indicate missing data.

So always validate values instead of assuming defaults are correct.


Best Practices

  • Always initialize variables explicitly when possible
  • Do not rely blindly on default values
  • Use nullable types when needed (e.g., int?)
  • Validate data before using it in business logic

Types of Data Types in C#

C# broadly classifies data types into three categories:

  • Value Types
  • Reference Types
  • Pointer Types

1. Value Types in C#

Value types store the actual value directly in memory.

These variables are stored in the stack memory.

When you assign one value type variable to another, the value is copied.


Example

int a = 10;
int b = a;

b = 20;

Console.WriteLine(a); // Output: 10
Console.WriteLine(b); // Output: 20

Here, changing b does not affect a because both hold separate copies.


Memory Size of Value Types in C#

Data Type Description Size
int Integer numbers 4 bytes
long Large integers 8 bytes
float Single precision decimal 4 bytes
double Double precision decimal 8 bytes
bool True/False 1 bit
char Single character 2 bytes

Real-Life Analogy

Think of value types like carrying a physical notebook.

If you give a copy of the notebook to someone, both of you now have separate copies.

Changes in one copy do not affect the other.


2. Reference Types in C#

Reference types store the memory address (reference) of the actual data instead of storing the value directly.

The actual data is stored in the heap memory.

When you assign one reference variable to another, both point to the same object.


Example

int[] arr1 = { 1, 2, 3 };
int[] arr2 = arr1;

arr2[0] = 100;

Console.WriteLine(arr1[0]); // Output: 100

Here, both arr1 and arr2 refer to the same memory location.

So a change through one affects the other.


Common Reference Types

  • string
  • arrays
  • classes
  • objects
  • interfaces
  • delegates

Real-Life Analogy

Think of reference types like a house address.

If two people have the same address, they are referring to the same house.

If someone changes the house (paint, furniture), everyone sees the change.


Garbage Collection (Important Concept)

Reference types are managed by the Garbage Collector in .NET.

When an object is no longer used, memory is automatically cleaned up.

This helps prevent memory leaks and improves application performance.


3. Pointer Types in C#

Pointer types store the memory address of another variable.

They are mainly used for low-level memory operations.

However, they are considered unsafe and are rarely used in modern C# applications.


Example

unsafe
{
    int x = 100;
    int* ptr = &x;

    Console.WriteLine(*ptr); // 100

    *ptr = 200;
    Console.WriteLine(x); // 200
}

When Are Pointers Used?

  • Performance-critical applications
  • Interoperability with unmanaged code
  • System-level programming

In most business applications, you will rarely need pointers.


Value Type vs Reference Type (Core Difference)

Feature Value Type Reference Type
Storage Stack Heap
Stores Actual Value Reference (Address)
Copy Behavior Value copied Reference copied
Performance Faster Slower (due to heap)

Common Mistakes Developers Make

  • Confusing value types with reference types
  • Using wrong data type (e.g., float instead of decimal for money)
  • Not understanding reference behavior in arrays and objects
  • Overusing pointer types unnecessarily

Best Practices

  • Always choose the correct data type based on requirement
  • Use decimal for financial calculations
  • Understand memory behavior (stack vs heap)
  • Avoid unsafe code unless absolutely required

Summary

Data types are one of the most critical concepts in C# programming.

They define how data is stored, accessed, and manipulated in memory.

Understanding value types, reference types, and pointer types helps you write efficient, safe, and scalable applications.

In the next chapter, we will explore Value Types and Reference Types in deeper detail with advanced examples.