Static readonly vs. const
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In C#, const and static readonly both represent values that should not change during normal program execution, but they are not interchangeable. The core difference is that const is a compile-time constant, while static readonly is a runtime-initialized field that can be assigned once and then treated as immutable.
What const Means
A const field is baked into compiled code at compile time. It must be assigned at the declaration site, and its value must be something the compiler can treat as a constant expression.
const fields are implicitly static, so you do not write static const in C#.
This is a good fit for values that are truly universal and not expected to vary between builds or assemblies.
What static readonly Means
A static readonly field is initialized at runtime, either inline or in a static constructor. After that initialization, it cannot be reassigned.
This allows values that are fixed after startup but not known at compile time.
The Versioning Difference Matters
One of the most important practical differences appears across assembly boundaries. Because const values are inlined into consuming assemblies at compile time, changing the constant in a library does not automatically update already compiled consumers. They must be recompiled.
static readonly does not have that problem because the value is read from the field at runtime.
That is why many public library APIs prefer static readonly unless the value is a true language-level constant such as a mathematical conversion factor or a token that will never change.
Type and Initialization Rules
const is limited to types the compiler can treat as constants, such as numeric primitives, char, bool, string, or null for reference types.
static readonly can be used with any type, including complex objects.
That makes static readonly the clear choice whenever the value is not a compile-time literal.
Practical Decision Rule
Use const when all of the following are true:
- the value is known at compile time
- the value is truly immutable in the conceptual sense
- inlining across assemblies is acceptable
Use static readonly when:
- the value is computed or loaded at runtime
- the type is not allowed for
const - you want consumers to observe updated values without recompilation
Common Pitfalls
- Using
constfor public library values that might change later can create versioning bugs because consuming assemblies inline the old value. Preferstatic readonlyfor externally consumed values unless the constant is truly permanent. - Assuming
constandstatic readonlyhave identical runtime behavior misses the compile-time inlining difference. That distinction is often the real reason to choose one over the other. - Reaching for
constwith non-literal types such asDateTimeorGuidwill not compile. Those cases requirereadonlyfields instead. - Treating
static readonlyas compile-time constant data can be misleading in switch cases or attributes where only true constants are allowed. Some language features requireconstspecifically. - Picking
static readonlyfor every literal value can be unnecessarily verbose when a simple compile-time constant is exactly what the language feature expects. Use the simpler tool when the semantics match.
Summary
- '
constis a compile-time constant and is implicitly static.' - '
static readonlyis assigned at runtime and then cannot be changed.' - '
constvalues are inlined into consuming assemblies, which affects versioning.' - '
static readonlyworks with any type and runtime initialization.' - Choose based on compile-time availability, type support, and whether future value changes must propagate without recompilation.
Related reading
- stop a thread before closing form
- Stop Parallel.ForEachAsync
- Storing WPF Image Resources
- Strange issue with System.Net.Http 4.2.0.0 not found
- Stream CopyToAsync never returns
- Stream.Seek0, SeekOrigin.Begin or Position 0
- String interning in .NET Framework - What are the benefits and when to use interning
- String vs string in C

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.