LearnThatStack Ace your next interview
C# · question
Question 2 of 112

What is the difference between const and readonly in C#?

beginner
← All C# questions
Re-explain
Visual ↓

A const is fixed at compile time, while a readonly field is fixed at run time, when its object or type is initialized.

  • A const must be initialized where it is declared and never changes. It is implicitly static and holds only primitive types, string literals or null.
  • A readonly field is set in its declaration or a constructor, so each instance can get its own value. It can be instance or static and hold any type.

For a reference type, readonly fixes only the reference. The object it points to can still change.

The compiler copies a const value into every caller, so changing it means recompiling those callers. A readonly value is read at run time. Use const for values that never change between versions, and readonly for values known only at run time.

public class Example
{
    const int MAX_SIZE = 100;           // Compile-time constant
    readonly DateTime createdDate;      // Runtime constant
    public Example()
    {
        createdDate = DateTime.Now;     // Can set in constructor
    }
}
Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
See it - interactive diagram
Why changing a const means rebuilding callers
App built against v1dotnet build App
App.dll
Config.Max100
Config.Limit1
Library.dll
1Config
const Max100
static readonly Limit100

The compiler copies the const Max value, 100, into App. App reads readonly Limit at run time.

Library v2 shippeddotnet build Library
App.dll
Config.Max100
Config.Limit1
Library.dll
1Config
const Maxchanged200
static readonly Limitchanged200

Library v2 sets Max and Limit to 200. Nobody rebuilt App, so Max stays at its copied 100, but Limit reads the new 200.

App rebuiltdotnet build App
App.dll
Config.Maxchanged200
Config.Limit1
Library.dll
1Config
const Max200
static readonly Limit200

Rebuilding copies the new 200 into App. Only now does Max match the library.

The compiler copies a const into each caller, so old callers keep the old value until rebuilt. Callers read a readonly field from the library at run time.

© LearnThatStack - diagrams may not be republished without permission.

Other questions have diagrams like this too. Sign in to ask for one where it's missing - it's free.

Create a free account
How well did you know this?
AI:

Saved in this browser - sign in to keep your review list.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Interview lens How interviewers actually use this question

Likely follow-ups

  • You ship a library with a public const and later change its value. What happens to apps that reference it? Callers keep the old value baked in until they are recompiled. A strong answer suggests public static readonly for values that may change in a later release.
  • A readonly field holds a List<T>. Can other code still add items to it, and how would you stop that? Yes, because readonly fixes only the reference. Name one fix, such as exposing a ReadOnlyCollection<T> or using an immutable collection.
  • Where can you use a const but not a static readonly field? Anywhere that needs a compile-time constant: attribute arguments, switch case labels and default values for optional parameters.
  • How does a readonly field compare with a get-only auto-property or an init-only property? A get-only auto-property is backed by a readonly field. An init accessor also allows object initializers. Properties keep encapsulation and can go in interfaces.

What you can say

  1. A const is fixed at compile time, and a readonly field is fixed at run time, when its object or type is initialized.
  2. A const has to get its value where it's declared. It's implicitly static and can only hold primitives, string literals or null.
  3. A readonly field can be set in its declaration or in a constructor, so each instance can get its own value, and it can be static or instance and hold any type.
  4. For a reference type, readonly only fixes the reference, so the object it points to can still change.
  5. The compiler copies a const value into every caller, so changing it means recompiling those callers, while a readonly value is read at run time.
  6. So I use const only for values that never change between versions, and readonly for anything known only at run time or that might change later.

Weak answers to avoid

  • Says readonly makes the object immutable readonly fixes only the reference. A readonly List<T> can still gain items, so say what readonly protects and what it doesn't.
  • Skips the recompile problem The compiler copies a const into every caller, so a changed public const leaves other assemblies on the old value until they are rebuilt. Interviewers listen for this.
  • Says a const can hold any type, like a DateTime A const can only hold primitives, string literals or null. A DateTime or any other object needs static readonly.
  • Recites syntax rules with no reason behind them A list of rules sounds memorized. Tie each rule back to compile time versus run time, then say when you would pick each one.
Interview lens

Likely follow-ups, what you can say, and the weak answers to avoid.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the C# cheatsheet.

← Back to all C# questions
Pro · $10/mo

98 of 112 C# answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime