0
0
CsharpComparisonBeginner · 3 min read

Const vs Readonly in C#: Key Differences and When to Use Each

In C#, const defines compile-time constants whose values cannot change and must be assigned at declaration, while readonly fields can be assigned once either at declaration or in a constructor and remain immutable afterward. const is static by default, but readonly can be instance-level or static.
⚖️

Quick Comparison

Here is a quick side-by-side comparison of const and readonly in C#.

Factorconstreadonly
Assignment TimeAt declaration onlyAt declaration or in constructor
Value ChangeNever changesCan be set once, then immutable
StorageImplicitly staticCan be instance or static
Type RestrictionsMust be primitive or compile-time constantAny type allowed
PerformanceReplaced at compile timeEvaluated at runtime
Use CaseFixed constants like PiValues set at runtime but constant after
⚖️

Key Differences

const fields are compile-time constants. This means their values are set when you write the code and cannot change later. They are implicitly static, so you access them through the class name, not an object instance. Because of this, const can only hold simple types like numbers, strings, or enums.

readonly fields are runtime constants. You can assign them when declaring or inside a constructor, allowing different instances to have different values. After assignment, their values cannot change. readonly fields can hold any type, including complex objects.

In summary, use const for fixed values known at compile time and readonly for values that are set once but only known at runtime.

⚖️

Code Comparison

This example shows how to use const to define a fixed constant value.

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

    public double GetCircumference(double radius)
    {
        return 2 * Pi * radius;
    }
}

class Program
{
    static void Main()
    {
        Circle c = new Circle();
        System.Console.WriteLine(c.GetCircumference(5));
    }
}
Output
31.4159
↔️

Readonly Equivalent

This example shows how to use readonly to set a value once at runtime, here in the constructor.

csharp
public class Circle
{
    public readonly double Pi;

    public Circle()
    {
        Pi = 3.14159;
    }

    public double GetCircumference(double radius)
    {
        return 2 * Pi * radius;
    }
}

class Program
{
    static void Main()
    {
        Circle c = new Circle();
        System.Console.WriteLine(c.GetCircumference(5));
    }
}
Output
31.4159
🎯

When to Use Which

Choose const when your value is truly constant and known at compile time, like mathematical constants or fixed configuration values. This makes your code faster and clearer.

Choose readonly when the value is set once but only known at runtime, such as values loaded from a file, database, or constructor parameters. This provides flexibility while ensuring immutability after initialization.

Key Takeaways

const is for compile-time fixed values and is implicitly static.
readonly allows runtime assignment once, either at declaration or in constructors.
const supports only simple types; readonly supports any type.
Use const for fixed constants and readonly for runtime-set immutable values.
readonly can be instance-level, while const is always static.