Programming
C# Language
Coding Practices
Variable Types
Software Development

Static readonly vs. const

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

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.

csharp
1public class AppSettings
2{
3    public const int DefaultPort = 8080;
4    public const string EnvironmentName = "Production";
5}

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.

csharp
1public class AppSettings
2{
3    public static readonly DateTime StartupReference = DateTime.UtcNow.Date;
4
5    public static readonly string ConfigPath;
6
7    static AppSettings()
8    {
9        ConfigPath = Environment.GetEnvironmentVariable("APP_CONFIG") ?? "/etc/myapp/config.json";
10    }
11}

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.

csharp
1public class Defaults
2{
3    public static readonly Guid EmptyGuid = Guid.Empty;
4    public static readonly TimeSpan RequestTimeout = TimeSpan.FromSeconds(30);
5}

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 const for public library values that might change later can create versioning bugs because consuming assemblies inline the old value. Prefer static readonly for externally consumed values unless the constant is truly permanent.
  • Assuming const and static readonly have identical runtime behavior misses the compile-time inlining difference. That distinction is often the real reason to choose one over the other.
  • Reaching for const with non-literal types such as DateTime or Guid will not compile. Those cases require readonly fields instead.
  • Treating static readonly as compile-time constant data can be misleading in switch cases or attributes where only true constants are allowed. Some language features require const specifically.
  • Picking static readonly for 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

  • 'const is a compile-time constant and is implicitly static.'
  • 'static readonly is assigned at runtime and then cannot be changed.'
  • 'const values are inlined into consuming assemblies, which affects versioning.'
  • 'static readonly works 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
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track 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.

Browse interview questions

All Rights Reserved.