readonly vs. const in C#: When to Use Which? In C#, both const and readonly are used to define immutable variables—values that, once set, cannot be changed. While they seem similar, they serve different purposes, have different lifetimes, and are initialized at different times. Choosing the wrong one can lead to unnecessary recompilation or runtime errors. 1. const (Compile-time Constants)
A const keyword declares a field whose value is fixed at compile time. The compiler replaces every usage of the constant with its actual value throughout your code.
Initialization: Must be initialized at the time of declaration. Evaluation Time: Compile-time.
Types: Limited to primitives (int, float, bool), strings, or null.
Access: Accessed via ClassName.VariableName (implicitly static). Example:
public const int MaxUsers = 100; public const string AppName = “MyApp”; Use code with caution. When to use const:
Values that are guaranteed never to change, such as physical constants (
, speed of light) or mathematical constants (e.g., DAYS_IN_WEEK).
When performance is critical, and you want the compiler to embed the value directly. 2. readonly (Runtime Readonly Fields)
A readonly field can only be assigned during declaration or inside a constructor. This means its value can be determined at runtime. Initialization: At declaration or in a constructor. Evaluation Time: Runtime. Types: Any type (objects, lists, etc.).
Access: Accessed via InstanceName.VariableName (unless combined with static). Example:
public class User { public readonly DateTime CreatedAt; public User() { CreatedAt = DateTime.Now; // Set at runtime } } Use code with caution. When to use readonly:
When the value is calculated at runtime (e.g., current time, API configuration). When the value depends on instance-specific data. When you are assigning a complex object or list. Key Differences Summary const readonly Initialization Only at declaration Declaration or Constructor Evaluation Compile-time Usage Context Primitive types, strings Memory Embedded directly in code Instance-specific (usually) static readonly – The Best of Both Worlds
You can use static with readonly to create a field that is initialized once at runtime, but shared across all instances of a class.
public static readonly Config AppConfig = LoadConfig(); // Runtime evaluation, global access Use code with caution. Decision Guide: When to Use Which?
Use const if the value is absolute, known at compile time, and will never change (e.g., int DaysInWeek = 7;).
Use readonly if the value depends on runtime information (e.g., reading a config file, generating a GUID) or if it’s an object/class type.
Use static readonly if you need a single, shared runtime value for all instances (e.g., static readonly Logger _log = new Logger();).
If you are confident the value of the constant won’t change, use a const. If you have a constant that may change (e.g., w.r.t. precision) or when in doubt, use a readonly.
If you’d like, I can provide a scenario-based quiz to test your understanding of which to use in different, tricky situations.
net – What is the difference between const and readonly in C
Leave a Reply