C# const vs readonly

Memory

const → compiler knows the value
readonly → runtime can set it, but cannot reassign it afterward

constreadonly
Java equivalentstatic final (compile-time constant)final
Known atCompile timeRuntime
AssignmentDeclaration onlyDeclaration / constructor
Instance field
Implicitly static
Runtime values
Can reassign later?
Typical useTrue constantsObject state / DI dependencies
public const int MaxRetries = 3;
 
private readonly IOrderRepository _repository;
 
public OrderService(IOrderRepository repository)
{
    _repository = repository;
}

private vs readonly

private controls who can access the field.
readonly controls whether the field can be reassigned.

private string _name;          // can be changed internally
private readonly string _name; // cannot be reassigned after construction

Java → C#

private final String name;  →  private readonly string _name;
 
static final int MAX = 10;  →  const int Max = 10;
 
static final Guid id =      →  static readonly Guid Id =
    UUID.randomUUID();          Guid.NewGuid();

readonly ≠ deep immutability

private readonly List<string> _items = new();
_items.Add("A"); // ✅
_items = new();  // ❌

readonly prevents reassignment, not mutation of the referenced object.