Static readonly or const in C#: What to Choose in 2025?

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: The Eternal Debate That Never Loses Relevance



Imagine this: you're writing C# code and need to declare a value that shouldn't change. Your first thought is const. Your second is static readonly. And your third is, "What's the actual difference?"



This question is one of the most popular on Stack Overflow. Thousands of developers, from beginners to seniors, continue to debate which is better. And in 2025, with the release of new versions of .NET and C# 13, this choice has become even more interesting.



In this article, we'll once and for all figure out when to use const and when to use static readonly. You'll learn not just the theory but also get clear rules you can apply starting today.



What Lies Behind the Modifiers?



const: A Rigid Constant

The const modifier in C# is literally a value "carved in stone." It must be known at compile time. The compiler substitutes the constant's value directly into the code wherever it is used.



public class MathConstants{    public const double Pi = 3.14159;    public const int DaysInWeek = 7;    public const string AppName = "MyApp";}


Note: const is always implicitly static. You cannot write static const — the compiler will throw an error.



static readonly: Flexible Immutability

The static readonly modifier is a field that is initialized once and never changes. The main difference: the value can be computed at runtime.



public class AppSettings{    public static readonly DateTime ReleaseDate = new DateTime(2025, 1, 15);    public static readonly Guid InstanceId = Guid.NewGuid();    public static readonly string ConfigPath = GetConfigPath();        private static string GetConfigPath()    {        return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "config.json");    }}


Key Differences: Quick Comparison Table



Let's break everything down in a clear table:



Characteristic const static readonly
Initialization Time Compilation Runtime (static constructor)
Allowed Types Only primitive (int, double, string, enum, bool) Any types (including reference types)
Memory Value is copied into each assembly Single reference in memory
Version Compatibility Issues when updating libraries Safe
Performance Maximum (compile-time substitution) Slight overhead on first access


When to Choose const?



Use const when:

  • The value is absolutely immutable — mathematical constants (π, e), number of days in a week, physical constants.
  • The value must be available at compile time — for example, for attributes, switch/case, default parameters.
  • You control all the code — and are sure the library won't be updated separately.


// Ideal for constpublic class Physics{    public const double SpeedOfLight = 299792458; // m/s    public const double GravitationalConstant = 6.67430e-11; // m³/(kg·s²)}

// Usage in an attribute —

Blogs

Book Recommendations