Last Updated on February 11, 2024


The readonly and const keywords are sometimes used interchangeably in C#, to declare immutable fields in a class or a structure. Even though we use them to achieve the same purpose, there are noticeable differences between these two keywords.

Readonly

In C#, the most common use case for the readonly keyword is to declare immutable fields in a class. For value type fields, that means the value cannot change during the lifecycle of the containing object. But for reference type fields, it means the reference cannot change during the lifecycle of the object: the field will always refer to the same object after initialization. In other words, only the reference of the object is immutable, but not the object itself.

When you use the readonly keyword in front of a field declaration, it simply means that you cannot modify this field after the object initialization. Basically, developers can initialize a readonly field only inline (at the declaration of the field) or inside one of the constructors of the class.

Here is a code sample where we are using readonly fields:

public class Movie
{
    private readonly string _title;

    private readonly int _year = DateTime.Now.Year;

    public string Title { get { return _title; } }

    public Movie(string title)
    {
        _title = title;
    }

    public Movie(string title, int year)
    {
        _title = title;
        _year = year;
    }
}

In this example, we are initializing the _year inline and the _title field only in the constructor.

When you try to update the readonly field outside of the constructor, you will see the following compiler error: CS0191 – a readonly field can only take an assignment in a constructor or at declaration.

c# readonly vs const: readonly keyword error

You can also use the readonly keyword in front of a struct definition. In such a case, it indicates that the structure type itself is immutable.

Moreover, you can also use it on methods definition of struct types. In this case, it indicates that the structure’s methods will not change the state of the structure. Here is an example of this use case:

public readonly struct MovieFinancialDetails
{
    public DateTime ReleaseDate { get; init; }

    public decimal Budget { get; init; }

    public decimal BoxOfficeToDate { get; init; }

    public readonly override  string ToString()
    {
        return $"(Budget={Budget}, BoxOfficeToDate={BoxOfficeToDate })";
    }
}




Const

In C#, developers use the const keyword to declare constant fields inside a class or constant variables inside a method. Since the declared field is a constant, the system does not expect the value to change during the lifecycle of the program. In other words, the value of the field is immutable.  

With the const keyword, the declared field can either be a string, a numeric value type, a boolean, or null (if it’s a reference type). You can initialize a constant field only inline at the declaration of the field. Contrary to the readonly field, you cannot initialize a constant field inside the constructor of a class. This restriction is due to the fact that constants are evaluated at compile-time and readonly fields are evaluated during runtime. Therefore, you cannot initialize a constant field to a value that is unknown before runtime, such as DateTime.Now for example.

public class CryptoLimitMarketOrder
{
    private const string _orderType="LIMIT_ORDER";

    public decimal Price { get; private set; }

    public decimal TriggerPrice { get; private set; }

    public decimal Quantity { get; private set; }


    public CryptoLimitMarketOrder(decimal price, decimal triggerPrice, decimal quantity)
    {
        Price = price;
        TriggerPrice = triggerPrice;
        Quantity = quantity;
    }

    public void PlaceBuyOrder()
    {
        const string side = "BUY";
        //TODO: Call Crypto API to place a Buy order
                
    }
}

Here is the compiler error when you try to initialize the constant field inside the constructor: CS0131 – The left-hand side of an assignment must be a variable, a property or indexer.

c# readonly vs const: const keyword error

Differences between C# readonly vs const

The following table gives a summary of the differences between the readonly keyword and the const keyword:

readonlyconst
DescriptionThe readonly keyword is used to declare read-only fields, immutable structures and struct members.The const keyword is used to declare constants either as fields or as local constants inside methods.
InitializationYou can initialize a readonly field only at the declaration of the field or in a constructor of the containing class.You can initialize a constant field only at the declaration of the field.
ParticularityReadonly fields are initialized at runtime.Constants are initialized at compile time. Therefore, if you change the value of the constant in a library, you need to recompile all programs using that library in order to use the new value of the constant.
Usage with the static keywordThe readonly keyword can be used with static fields.The const keyword cannot be used with static fields.