0
0
CsharpComparisonBeginner · 3 min read

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#.

Featureconststatic readonly
Initialization TimeCompile-time (must be a compile-time constant)Runtime (can be assigned in declaration or static constructor)
Value ChangeCannot change after compilationCan be assigned once, typically in static constructor
Allowed TypesOnly primitive types, enums, or stringsAny type including reference types
Memory LocationEmbedded in compiled code (inlined)Stored in memory, accessed via static field
Use CaseFixed constants like mathematical valuesConstants 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

csharp
public class ConstantsExample
{
    public const double Pi = 3.14159;

    public static void Main()
    {
        System.Console.WriteLine($"Const Pi: {Pi}");
    }
}
Output
Const Pi: 3.14159
↔️

Static Readonly Equivalent

csharp
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}");
    }
}
Output
Static Readonly Pi: 3.141592653589793
🎯

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.
Use const for simple, unchanging values; use static readonly for runtime or complex constants.