const vs readonly — Deep Dive

The Core Difference

const

Compile-time constant

Compiler knows the value

Implicitly static
 
readonly

Runtime value

Assigned during initialization/construction

Cannot be reassigned afterward

private vs readonly

These solve different problems:

private
  → Who can access it?
 
readonly
  → Can it be reassigned?

Therefore:

private readonly string _connectionString;

means:

Only this class can access it, and it can only be assigned during initialization/construction.

Getter vs Setter

Read-only property

public string Name { get; }
 
public User(string name)
{
    Name = name;
}

Outside code:

user.Name;       // ✅
user.Name = "X"; // ❌

Good when the value should never change after construction.

Internally mutable property

public string Name { get; private set; }
 
public User(string name)
{
    Name = name;
}

Outside:

user.Name = "X"; // ❌

Inside the class:

Name = "X"; // ✅

Don't confuse these

get; = publicly readable, not necessarily a readonly field.

get; private set; = outside cannot modify, but the class can.

readonly field = field cannot be reassigned after construction.

Why readonly is useful in ASP.NET Core

Common dependency-injection pattern:

public class OrderService
{
    private readonly IOrderRepository _repository;
 
    public OrderService(IOrderRepository repository)
    {
        _repository = repository;
    }
}

The dependency is established when the object is created and should not be replaced later.

Production Rule

Use:

const
→ value is genuinely a compile-time invariant
 
readonly
→ value comes from runtime state but should not be reassigned
 
get; private set;
→ class needs to control future mutations
 
get;
→ value should be established during construction and then exposed read-only

Public const and versioning

const values can be embedded into consuming assemblies at compile time. Changing a public constant in a library may therefore require consumers to be recompiled.

static readonly is preferable when the value is runtime-initialized or may change independently of consumers.