C# - Difference Between Const and ReadOnly


There are many different keywords available in C#. This tutorial will look at the differences between the readonly and const keywords in C# and when to use each.

Const in C#

By default, a const is static. In C#, a constant is a value that cannot be changed during the execution or run time of a program. That means value of Const will not change during whole program. It's mandatory to assign a value to it when we declared it. 

Readonly in C#

A Readonly field can be initialized either at the time of declaration or within the constructor of the same class. We can also change the value of a Readonly at runtime or assign a value to it at runtime (but in a non-static constructor only).

Difference

  • Initialization

    • const - Must be initialized at the time of declaration and cannot be changed afterwards.
      public const int MaxValue = 100; 
    • readonly - Can be initialized either at the time of declaration or within a constructor. This allows for different values to be assigned to different instances of a class.
      public readonly int MaxValue;
      public MyClass(int maxValue)
      {
          MaxValue = maxValue; // Allowed in constructor
      } 
  • Value Type

    • const - The value must be a compile-time constant, such as a number, string, or other primitive type.
      public const double Pi = 3.14159;
    • readonly -The value can be any type, including objects, and can be determined at runtime.
      public readonly DateTime CreationTime = DateTime.Now;
  • Scope

    • const - Implicitly static, meaning it belongs to the type itself and not to any instance. It is shared across all instances.
      public const int MaxValue = 100;
    • readonly - Can be either instance-specific or static. If declared as static, it is shared across all instances; if not, each instance can have its own value.
      public readonly int MaxValue; // Instance-specific
      public static readonly int GlobalValue = 100; // Shared across all instances

  • Mutability

    • const - Completely immutable after declaration.
    • readonly - Immutable after initialization (either at declaration or in the constructor).

When to use Const and Readonly?

  • const

    • Suitable for fixed values known at compile time.
    • Implicitly static, meaning they belong to the type itself and are shared among all instances.
    • Must be initialized at declaration and cannot change afterwards.
  • readonly

    • Suitable for values that should not change after initialization but are not known until runtime.
    • Can be instance-specific or static.
    • Can be initialized at declaration or in a constructor.

Difference between Const and ReadOnly

S.No. Const ReadOnly
1 A Const can only initialized at the time of declaration. A ReadOnly can be initialized at the time of declaration or within the constructor of the same class.
2 A Const field is a compile time constant. A ReadOnly field is used as a run time constant.
3 It can be access using classname.constname It can be access using Instance
4 Once a value is assigned to a constant. It can not be initialized again. You can change ReadOnly value number of times. You can also change it inside constructor.
5 Example - public const int MaxValue = 100; Example - public readonly int MaxValue = 100;

Prev Next