Difference between const and static readonly
In C#, the difference between static readonly and const is as follows:
static readonly:Is a runtime constant.
Its value can be initialized either at the time of declaration or in a static constructor, and it cannot be changed thereafter.
It is accessed via the class name, and its memory is allocated only once.
It can be of any type, including reference types.
const:Is a compile-time constant.
Its value must be initialized when declared and cannot be changed at runtime.
It is accessed via the class name, and its memory is also allocated only once.
It can only be used with primitive types or other compile-time constants.
The main difference is that a static readonly field can be initialized at runtime, whereas a const must be initialized at compile time.
Exploring the Differences Through Code
Original C# Code Example
Compiled Low-Level C# Code
Below is an illustration of what the compiled code might resemble:
Explanation
constVariables:
When the compiler encounters a constant string, it embeds the constant's value directly into the code wherever it is referenced. This eliminates additional memory overhead and provides fast access times.static readonlyVariables:
The compiler generates a static constructor for the class to initializestatic readonlyfields at runtime, during the class's first use. Although this introduces a slight overhead at first use, it allows for runtime initialization and supports non-primitive types.
Performance Considerations
constVariables:
Offer better performance since their values are embedded at compile time, resulting in direct, inlined values with no extra memory access.static readonlyVariables:
Require a static constructor to initialize them at runtime, introducing a small overhead on first use, but they offer more flexibility regarding initialization and type usage.
Note: Using static readonly for a collection such as a List<T> ensures that the reference remains constant; however, it does not prevent modification of the collection's contents at runtime.
Conclusion
Both static readonly and const are useful for declaring constants in C#, but they serve different purposes:
Use
constwhen:The value is truly constant and can be determined at compile time.
You are working with primitive types or compile-time constants.
You require maximum performance with no runtime overhead.
Use
static readonlywhen:The value needs to be determined at runtime.
You are working with complex types or reference types.
You need the flexibility of runtime initialization despite a small initial overhead.
See Also:
Code Review Checklist (mentions const/readonly)
Maximizing Performance and Readability with const Variables in C#
Static Methods (related 'static' concept)
Transitioning from Classes to Records and Adopting Immutable Collections in C# (related to immutability)