C#  

Difference Between readonly and const in C# with Examples?

Introduction

When working with C#, developers often come across two keywords that look similar but behave quite differently: readonly and const.

At first glance, both seem to represent values that cannot be changed, but their behavior, usage, and purpose are not the same.

Understanding the difference between readonly vs const in C# is important for writing clean, maintainable, and predictable code—especially in real-world applications.

In this article, we will break down both concepts in a clear and practical way, along with examples, use cases, and common mistakes.

What is const in C#?

A const (constant) is a value that is fixed at compile time and cannot be changed anywhere in the program.

Key Characteristics of const

  • Value must be assigned at the time of declaration

  • Value is known at compile time

  • Cannot be modified later

  • By default, it is static (shared across all instances)

Example of const in C#

public class Example
{
    public const int MaxUsers = 100;
}

Explanation

  • MaxUsers is assigned a value at compile time

  • This value is embedded directly into the compiled code

  • It cannot be changed at runtime or in constructors

Real-world Example

public class AppSettings
{
    public const string AppName = "MyApplication";
}

This is useful for values that never change, like application name, mathematical constants, or fixed configuration values.

What is readonly in C#?

A readonly field is a variable whose value can only be assigned at runtime, but only once—either during declaration or inside a constructor.

Key Characteristics of readonly

  • Value can be assigned at runtime

  • Can be assigned in constructor

  • Cannot be modified after initialization

  • Not necessarily static

Example of readonly in C#

public class User
{
    public readonly DateTime CreatedAt;

    public User()
    {
        CreatedAt = DateTime.Now;
    }
}

Explanation

  • CreatedAt is assigned when the object is created

  • Each object can have a different value

  • Once set, it cannot be changed

Real-world Example

public class Order
{
    public readonly Guid OrderId;

    public Order()
    {
        OrderId = Guid.NewGuid();
    }
}

Each order gets a unique ID at runtime, which cannot be modified later.

Key Differences Between readonly and const in C#

1. Time of Assignment

  • const → Assigned at compile time

  • readonly → Assigned at runtime

2. Where Value is Set

  • const → Must be set during declaration

  • readonly → Can be set in constructor

3. Flexibility

  • const → Completely fixed

  • readonly → Flexible during object creation

4. Static Behavior

  • const → Always static

  • readonly → Can be instance-level or static

5. Use Case

  • const → Fixed values (PI, limits, strings)

  • readonly → Values depending on runtime logic

Code Comparison Example

public class Demo
{
    public const int ConstValue = 10;
    public readonly int ReadonlyValue;

    public Demo(int value)
    {
        ReadonlyValue = value;
    }
}

Explanation

  • ConstValue is fixed and shared across all objects

  • ReadonlyValue can vary for each instance

When to Use const in C#

Use const when:

  • Value is known at compile time

  • Value will never change

  • Value is universal across application

Examples

  • Mathematical constants

  • Fixed strings

  • Configuration flags

When to Use readonly in C#

Use readonly when:

  • Value depends on runtime logic

  • Value should not change after initialization

  • Each object may have different values

Examples

  • Database IDs

  • Timestamps

  • Configuration loaded at runtime

Common Mistakes Developers Make

Using const for runtime values

This leads to compilation errors because const requires compile-time values.

Overusing readonly for fixed values

If a value never changes, const is more appropriate and efficient.

Forgetting that const is static

Many developers assume const behaves like instance variables, which is incorrect.

Advantages of const

  • Faster performance (compile-time optimization)

  • Memory efficient

  • Easy to use for fixed values

Advantages of readonly

  • More flexible

  • Supports runtime initialization

  • Safer for dynamic scenarios

Summary

The difference between readonly and const in C# lies in when and how their values are assigned. Const is used for values that are completely fixed and known at compile time, while readonly is used for values that are assigned at runtime but should not change afterward. Choosing the right keyword helps improve code clarity, performance, and maintainability in ASP.NET Core and C# applications.