const → compiler knows the value readonly → runtime can set it, but cannot reassign it afterward
const
readonly
Java equivalent
static final (compile-time constant)
final
Known at
Compile time
Runtime
Assignment
Declaration only
Declaration / constructor
Instance field
❌
✅
Implicitly static
✅
❌
Runtime values
❌
✅
Can reassign later?
❌
❌
Typical use
True constants
Object 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 internallyprivate 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();