1. Core Difference
| Feature | const | readonly |
|---|---|---|
| When value is determined | Compile time | Runtime |
| Assignment | Declaration only | Declaration or constructor |
| Can change after initialization? | ❌ Never | ❌ After construction |
| Instance field | ❌ No | ✅ Yes |
Implicitly static | ✅ Yes | ❌ No |
| Can use method result? | ❌ No | ✅ Yes |
| Can use constructor parameter? | ❌ No | ✅ Yes |
| Can use runtime configuration? | ❌ No | ✅ Yes |
| Supported types | Limited compile-time types | Any type |
| Typical use | True constants | Runtime immutable state |
Memory trick
CONST → Compiler knows it.
READONLY → Runtime can know it, but nobody can change it afterward.
2. const
public const int MaxRetries = 3;
public const string ApplicationName = "PaymentService";A const value must be known at compile time.
Why?
Because the compiler needs to know the value while compiling the code.
public const int MaxRetries = 3; // ✅
public const string Name = "Payment"; // ✅
public const int Value = GetValue(); // ❌
public const DateTime Now = DateTime.Now; // ❌GetValue() and DateTime.Now require runtime execution.
3. readonly
public class PaymentService
{
private readonly string _connectionString;
public PaymentService(string connectionString)
{
_connectionString = connectionString;
}
}The value can be determined at runtime, but after construction it cannot be reassigned.
var service = new PaymentService("Server=prod");
service._connectionString = "Server=test"; // ❌Why?
Because readonly is designed for runtime initialization followed by immutability.
4. Why Do We Need Both?
Question
Why not just use
readonlyfor everything?
Answer
Because const communicates something stronger:
This value is a compile-time constant and is part of the type’s fixed definition.
Example:
public const int MaxItemsPerPage = 100;Whereas:
public readonly int MaxItemsPerPage;means:
This value is assigned at runtime but won’t change afterward.
Mental model
Is the value known at compile time?
│
┌─────────┴─────────┐
YES NO
│ │
const readonly
│ │
Compile-time Runtime initialization
constant + immutable afterward5. Is const Automatically static?
Yes.
public class Configuration
{
public const int MaxRetries = 3;
}Access it directly:
Configuration.MaxRetries;You don’t need an instance:
var config = new Configuration();
config.MaxRetries; // ❌Why?
A const represents one compile-time value associated with the type, not with individual objects.
Therefore, const fields are implicitly static.
6. readonly Instance Field
public class User
{
public readonly Guid Id;
public User()
{
Id = Guid.NewGuid();
}
}Each object can have a different value:
User A
└── Id = AAA
User B
└── Id = BBB
User C
└── Id = CCCWhy?
readonly is an instance field by default.
Each object gets its own field.
7. static readonly
public class Application
{
public static readonly Guid ApplicationId = Guid.NewGuid();
}There is one value for the entire type.
Application
│
└── ApplicationId
│
└── ONE valueWhy?
static controls ownership.
readonly controls reassignment.
Therefore:
static
↓
One field for the type
readonly
↓
Cannot reassign after initialization8. readonly Can Use Runtime Values
public readonly Guid Id = Guid.NewGuid();or:
public class Order
{
public readonly DateTime CreatedAt;
public Order()
{
CreatedAt = DateTime.UtcNow;
}
}Why?
The value doesn’t need to be known by the compiler.
It only needs to be assigned during initialization/construction.
9. Why Can’t const Use Constructor Parameters?
This is invalid:
public class Server
{
public const string HostName;
public Server(string hostName)
{
HostName = hostName; // ❌
}
}Why?
The constructor executes at runtime.
But const requires a value known at compile time.
Use:
public readonly string HostName;
public Server(string hostName)
{
HostName = hostName;
}10. const and DateTime
This is invalid:
public const DateTime CreatedAt = DateTime.UtcNow;Why?
DateTime.UtcNow is evaluated at runtime.
DateTime is also not a valid const field type.
Use:
public static readonly DateTime CreatedAt = DateTime.UtcNow;Interview answer
constsupports only types whose values can be represented as compile-time constants.DateTimerequires runtime representation, so usereadonly.
11. Can readonly Be Modified?
This is an important interview trap.
public class User
{
public readonly List<string> Roles = new();
}You cannot replace the reference:
Roles = new List<string>(); // ❌But you can modify the object:
Roles.Add("Admin"); // ✅
Roles.Remove("Admin"); // ✅Why?
readonly protects the field from reassignment, not necessarily the object referenced by the field.
readonly field
│
▼
┌───────────┐
│ List │
│ │
│ Admin │
│ User │
└───────────┘
Reference cannot change
↓
❌
Object contents can change
↓
✅Important
readonlydoes NOT automatically mean the referenced object is immutable.
12. Is readonly the Same as Immutable?
No.
private readonly List<string> _users;means:
_userscannot point to another list after initialization.
It does not mean:
The list itself cannot change.
For true immutability, use immutable types/collections or design the object accordingly.
Why?
Because immutability is a property of the object/state, whereas readonly is primarily a restriction on field reassignment.
13. const vs Configuration
Bad choice
public const string ConnectionString =
"Server=production-db;";Better
private readonly string _connectionString;
public PaymentService(IConfiguration configuration)
{
_connectionString =
configuration.GetConnectionString("Payments")!;
}Why?
Configuration differs by environment:
Development → dev-db
QA → qa-db
Production → prod-dbThe value isn’t known at compile time.
Therefore, it should not be a const.
14. Production Example
Consider a retry policy.
Compile-time invariant
public const int MaxPageSize = 100;Good candidate for const.
Runtime configuration
private readonly int _maxRetries;
public PaymentService(IConfiguration configuration)
{
_maxRetries =
configuration.GetValue<int>("Payment:MaxRetries");
}Good candidate for readonly.
Why?
MaxPageSize
↓
Fixed by application design
↓
const
MaxRetries
↓
May differ by environment
↓
readonly15. Important Senior-Level Difference: Assembly Versioning
This is one of the best interview questions.
Suppose a shared library contains:
public const int MaxRetries = 3;Another application references it:
Console.WriteLine(Config.MaxRetries);Now change the library:
public const int MaxRetries = 5;and deploy only the library.
The consuming application may still use:
3until it is recompiled.
Why?
Because the compiler can substitute/embed the const value into the consuming assembly.
Conceptually:
Library
│
│ const = 3
▼
Compiler
│
▼
Consumer Assembly
│
└── effectively contains 3With:
public static readonly int MaxRetries = 3;the value is read from the field at runtime.
Interview takeaway
Be careful with
public constvalues in shared libraries when the value may change independently of consumers.
static readonlycan avoid this particular compile-time substitution/versioning issue.
16. Can readonly Be Assigned in a Method?
Generally, no.
public class User
{
public readonly string Name;
public User()
{
Name = "Sameer";
}
public void ChangeName()
{
Name = "John"; // ❌
}
}Why?
A readonly instance field can be assigned:
- At declaration
- In the instance constructor
After construction, reassignment is prohibited.
17. What About static readonly?
Example:
public class Application
{
public static readonly string Environment;
static Application()
{
Environment = "Production";
}
}Why?
A static readonly field can be initialized:
- At declaration
- In the static constructor
After static initialization, it cannot be reassigned.
18. Interview Comparison
| Interview Question | const | readonly |
|---|---|---|
| Compile-time value? | ✅ | ❌ |
| Runtime initialization? | ❌ | ✅ |
| Constructor assignment? | ❌ | ✅ |
| Instance member? | ❌ | ✅ |
| Implicitly static? | ✅ | ❌ |
| Method call during initialization? | ❌ | ✅ |
| Configuration value? | ❌ | ✅ |
| Can referenced object mutate? | N/A | ✅ Potentially |
| Suitable for public library changing values? | ⚠️ Be careful | ✅ Usually safer |
| Prevents field reassignment? | ✅ | ✅ |
19. Common Interview Questions
Q1. What is the difference between const and readonly?
constis evaluated at compile time and is implicitly static.readonlyis evaluated at runtime and can be assigned during declaration or construction, after which the field cannot be reassigned.
Why?
Because const represents a compile-time invariant, while readonly represents runtime state that becomes immutable after initialization.
Q2. Why is const implicitly static?
Because a constant belongs to the type rather than to individual instances.
Why?
There is no reason for every object to have a separate copy of a compile-time constant.
Q3. Can readonly be assigned in a constructor?
Yes.
Why?
Because readonly allows runtime initialization but prevents reassignment after object construction.
Q4. Can const be assigned in a constructor?
No.
Why?
A constructor executes at runtime, while const requires compile-time evaluation.
Q5. Can a readonly List<T> be modified?
Yes.
readonly List<string> users = new();
users.Add("John"); // ✅Why?
readonly prevents changing the reference, not mutating the referenced object.
Q6. Is readonly the same as immutable?
No.
Why?
readonly protects the field reference from reassignment. It does not guarantee that the referenced object cannot change.
Q7. Can const contain DateTime.Now?
No.
Why?
DateTime.Now is evaluated at runtime.
Q8. Can readonly contain DateTime.Now?
Yes.
public readonly DateTime CreatedAt = DateTime.Now;Why?
readonly supports runtime initialization.
Q9. When would you choose static readonly over const?
When the value is fixed after initialization but must be calculated or obtained at runtime.
Example:
public static readonly Guid ApplicationId = Guid.NewGuid();Why?
Guid.NewGuid() cannot execute at compile time.
Q10. Which should I use for application configuration?
Usually configuration binding/options or a
readonlyfield, notconst.
Why?
Configuration is typically environment-specific and therefore isn’t known at compile time.
20. The 30-Second Interview Answer
constis a compile-time constant and is implicitly static. Its value must be known when the code is compiled.readonlyis a runtime field that can be initialized at declaration or in a constructor, but cannot be reassigned afterward.I use
constfor true compile-time invariants such as fixed limits or mathematical constants. I usereadonlywhen the value needs to come from runtime state, configuration, dependency injection, or object construction.One important distinction is that
readonlydoesn’t make the referenced object immutable—it only prevents reassignment of the field.
21. Remember This
const vs readonly
┌───────────────────────┐
│ Is value known at │
│ compile time? │
└───────────┬───────────┘
│
┌────────┴────────┐
│ │
YES NO
│ │
▼ ▼
const readonly
│ │
│ │
Compiler knows Runtime knows
the value the value
│ │
▼ ▼
Implicitly static Instance by default
│ │
▼ ▼
Never reassigned Assign during
initialization/
construction
│
▼
Never reassign
afterwardFinal memory rule
constanswers: “Can the compiler know this value?”
readonlyanswers: “Can I allow this value to be determined at runtime, but prevent reassignment afterward?”If you remember those two questions, most
constvsreadonlyinterview questions become easy.