C#
Programming
Coding Concepts
Software Development
C# Variables

What is the difference between const and readonly in C#?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

const and readonly both express immutability in C#, but they mean different things at different stages of execution. const is a compile-time constant baked into consuming code, while readonly is a runtime-assigned field that can be set once during initialization or construction.

const Means Compile-Time Constant

A const field must be assigned a value that the compiler can determine immediately.

csharp
1public class Limits
2{
3    public const int MaxRetries = 3;
4    public const string AppName = "Importer";
5}

Because the value is known at compile time, the compiler substitutes the literal directly into calling code.

csharp
Console.WriteLine(Limits.MaxRetries);

That substitution is important. If another assembly references MaxRetries, it may embed the value 3 at compile time. Changing the library later without recompiling the consumer can leave old callers using the old constant value.

That is why const is best for values that are truly universal and stable, such as mathematical or protocol constants.

readonly Means Assigned Once at Runtime

A readonly field can be assigned:

  • at declaration time
  • in the constructor
  • in a static constructor if it is static readonly
csharp
1public class Settings
2{
3    public readonly int TimeoutSeconds;
4
5    public Settings(int timeoutSeconds)
6    {
7        TimeoutSeconds = timeoutSeconds;
8    }
9}

This makes readonly much more flexible. The value is still immutable after construction, but it does not need to be a compile-time literal.

readonly also works with values computed at runtime:

csharp
1public class BuildInfo
2{
3    public static readonly DateTime StartupUtc = DateTime.UtcNow;
4}

That could never be a const, because DateTime.UtcNow is determined at runtime.

const Is Implicitly Static, readonly Is Not

This is a small but important difference. A const belongs to the type itself, not to each instance.

csharp
1public class Example
2{
3    public const int A = 1;
4    public readonly int B;
5
6    public Example(int b)
7    {
8        B = b;
9    }
10}

A has one fixed meaning for the whole type. B can differ per instance, but once each instance is constructed, its own B cannot change.

If you want one shared immutable runtime value, use static readonly.

csharp
public static readonly Guid ApplicationId = Guid.NewGuid();

Versioning and Public APIs

This is where the distinction becomes operationally important. Public const fields can be risky across assemblies because callers may inline the value. If you expect the value might change in future versions, prefer public static readonly.

csharp
1public class ApiDefaults
2{
3    public static readonly TimeSpan DefaultTimeout = TimeSpan.FromSeconds(30);
4}

This keeps callers reading the field from the assembly at runtime instead of baking the old value into their own build.

A practical rule:

  • 'const for values that are permanently fixed'
  • 'readonly for values that are immutable after initialization but may depend on runtime or versioning concerns'

Reference Types and Object State

A readonly reference means the field cannot point to a different object after initialization. It does not mean the object itself is immutable.

csharp
1using System.Collections.Generic;
2
3public class Catalog
4{
5    public readonly List<string> Names = new List<string>();
6}

The Names field cannot be reassigned to a new list, but code can still add items to the existing list unless you choose an immutable collection type.

That subtlety matters when people assume readonly gives deep immutability. It does not.

Common Pitfalls

  • Using const for public library values that may change later creates versioning surprises because consumers can inline the old value.
  • Expecting readonly to mean compile-time constant misses the fact that constructor-assigned values are allowed.
  • Forgetting that const is implicitly static can make the type semantics less obvious to newer developers.
  • Assuming a readonly reference makes the referenced object immutable leads to accidental mutation bugs.
  • Using const for values that are really configuration or deployment choices makes the code less flexible than it should be.

Summary

  • 'const is compile-time, literal, and implicitly static.'
  • 'readonly is runtime-assigned once, usually in a declaration or constructor.'
  • Use const for true constants that should never vary.
  • Use readonly or static readonly when the value is immutable after initialization but not a compile-time literal.
  • For public APIs and libraries, prefer static readonly over const when future value changes are possible.

Course illustration
Course illustration

All Rights Reserved.