Const vs static readonly in C#: Key Differences and Usage
const defines compile-time constants that cannot change and must be initialized with a value known at compile time, while static readonly defines runtime constants that can be assigned once at runtime, typically in a static constructor. Use const for fixed values and static readonly for values that require runtime calculation or reference types.Quick Comparison
This table summarizes the main differences between const and static readonly in C#.
| Feature | const | static readonly |
|---|---|---|
| Initialization Time | Compile-time (must be a compile-time constant) | Runtime (can be assigned in declaration or static constructor) |
| Value Change | Cannot change after compilation | Can be assigned once, typically in static constructor |
| Allowed Types | Only primitive types, enums, or strings | Any type including reference types |
| Memory Location | Embedded in compiled code (inlined) | Stored in memory, accessed via static field |
| Use Case | Fixed constants like mathematical values | Constants requiring runtime calculation or complex types |
Key Differences
const fields are replaced by their values at compile time, so they must be initialized with values known before the program runs. This means you cannot assign const fields using variables or method calls. They are implicitly static and cannot be changed later.
On the other hand, static readonly fields are evaluated at runtime and can be assigned either at declaration or inside a static constructor. This allows you to use runtime information or method results to set their values. They are not inlined, so changes to their values after recompilation are reflected without recompiling dependent code.
Another important difference is that const supports only primitive types, enums, or strings, while static readonly can hold any type, including complex objects and reference types.
Code Comparison
public class ConstantsExample { public const double Pi = 3.14159; public static void Main() { System.Console.WriteLine($"Const Pi: {Pi}"); } }
Static Readonly Equivalent
public class ReadonlyExample { public static readonly double Pi; static ReadonlyExample() { Pi = System.Math.PI; } public static void Main() { System.Console.WriteLine($"Static Readonly Pi: {Pi}"); } }
When to Use Which
Choose const when you have a value that is truly constant, known at compile time, and unlikely to ever change, such as mathematical constants or fixed configuration values. This offers better performance because the value is inlined.
Choose static readonly when the value needs to be computed at runtime, depends on other variables, or is a reference type. It also allows you to change the value by recompiling only the defining assembly without affecting dependent assemblies.
Key Takeaways
const is for compile-time fixed values and is implicitly static.static readonly allows runtime initialization and supports any type.const values are inlined, so changing them requires recompiling all dependent code.static readonly values are stored in memory and can be assigned in constructors.const for simple, unchanging values; use static readonly for runtime or complex constants.