What is the difference between const, readonly, and static in C#?
Understand const, readonly, and static in C#: compile-time vs runtime constants, instance vs shared members, the recompile trap, and when to use each.
Expected Interview Answer
const is a compile-time constant baked into callers and implicitly static; readonly is a runtime constant assignable only in its declaration or constructor; static means a member belongs to the type itself rather than any instance.
const values must be known at compile time and are substituted directly wherever used, so changing one requires recompiling every referencing assembly. readonly is evaluated at runtime, so it can hold computed values or constructor arguments and each instance may hold a different value. static is an orthogonal concept: it controls whether a member is shared across all instances, and you can combine static with readonly (but const is already static).
- const gives fast, inlined, truly fixed values
- readonly allows runtime and per-instance immutable values
- static shares one member across all instances
- static readonly avoids the recompile trap of const
- Clearer intent about what can change and when
AI Mentor Explanation
const is a boundary rope fixed at exactly the printed distance and painted onto every scorecard in advance; readonly is the pitch length measured and locked on match morning; static is the single shared scoreboard the whole ground reads, one value for everyone rather than each player keeping a private tally.
Step-by-Step Explanation
Step 1
Use const for true compile-time values
Declare values known at compile time like Pi or a version string; they are implicitly static.
Step 2
Use readonly for runtime constants
Assign in the declaration or constructor for values computed at runtime or passed to the constructor.
Step 3
Use static for shared members
Mark a field or method static when it belongs to the type, shared across all instances.
Step 4
Prefer static readonly over public const
For values referenced across assemblies, static readonly avoids forcing consumers to recompile.
Step 5
Combine deliberately
static readonly for shared immutable runtime values; const is already static so cannot be marked static.
What Interviewer Expects
- Knows const is implicitly static and compile-time
- Understands readonly is evaluated at runtime
- Can explain the const cross-assembly recompile trap
- Recognizes static is orthogonal to immutability
- Knows when to choose static readonly over const
Common Mistakes
- Thinking readonly means the same as const
- Not realizing const bakes the value into the caller
- Using public const for values that might change across assemblies
- Believing static implies immutable
- Trying to mark const as static explicitly
Best Answer (HR Friendly)
“const is a value fixed at compile time and never changes; readonly is set once when an object is created and then stays fixed; static means the value is shared by the whole class instead of each object having its own copy.”
Code Example
public class Circle
{
public const double Pi = 3.14159; // compile-time, implicitly static
public static readonly DateTime Loaded = DateTime.Now; // runtime, shared
public readonly double Radius; // set per instance in constructor
public static int Count; // shared mutable counter
public Circle(double radius)
{
Radius = radius; // allowed: readonly assigned in constructor
Count++;
}
public double Area() => Pi * Radius * Radius;
}
// Pi is inlined into callers; Radius differs per instance;
// Count and Loaded are shared across all Circle objects.Follow-up Questions
- Why can changing a public const break other assemblies?
- When would you choose static readonly over const?
- Can a readonly field be modified inside a method other than the constructor?
- Is a const field stored per instance or once for the type?
- What is the difference between static readonly and a get-only property?
MCQ Practice
1. When is a const value evaluated in C#?
const values must be known at compile time and are substituted directly into the code that references them.
2. Where can a readonly field be assigned?
A readonly field may be assigned at its declaration or within a constructor, then it cannot change afterward.
3. Which statement about static is correct?
static means the member belongs to the type itself and is shared; it does not imply immutability.
Flash Cards
Is const static? — Yes, const is implicitly static; you cannot and need not add the static keyword.
When is readonly assigned? — In its declaration or in a constructor, at runtime, and it may differ per instance.
Why prefer static readonly over public const? — const is inlined into callers, so changing it requires recompiling every referencing assembly; static readonly does not.
Does static mean immutable? — No. static controls sharing across instances; a static field can still be mutable.