C# .NET - What is the difference between Constant and readonly

Asked By aman on 10-Sep-11 02:49 AM
hi all,

can any one help me in finding the differences between Constant and readonly keywords in C#

please give me some example so that i can make use of it...
Devil Scorpio replied to aman on 10-Sep-11 02:57 AM
Hi Aman,

Const Keyword in C# .NET

* Example: public const string abc = “xyz”;
* Initialized only at declaration.
* Value is evaluated at compile time and can not be changed at run time.
An attempt to change it will cause a compilation error.
* Const is already kind of static.
* Since classes and structs are initialized at run time with new keyword, you can’t set a constant to a class or structure. But, it has to be one of the integral types.

Readonly Keyword in C# .NET

* Example: public readonly string abc;
* Can be initialized in declaration code or consturctor code.
* Value is evaluated at run time.
* Can be declared as static or instance level attribute.
* A read only field can hold a complex object by using the new keyword at run time.

Reference :- http://www.carlj.ca/2009/03/09/whats-the-difference-between-const-and-readonly-in-net/
Sharad Chandra replied to aman on 10-Sep-11 04:35 AM

Apart from the apparent diff of

  • having to declare the value at the time of a definition for a const VS readonly values can be computed dynamically but need to be assigned before the ctor exits.. after that it is frozen.
  • 'const's are implicitly static. You use a ClassName.ConstantName notation to access them.

There is a subtle difference. Consider a class defined in AssemblyA.

public class Const_V_Readonly
public const int I_CONST_VALUE = 2;
public readonly int I_RO_VALUE;
public Const_V_Readonly()
= 3;

AssemblyB references AssemblyA and uses these values in code. When this is compiled,

  • in the case of the const value, it is like a find-replace, the value 2 is 'baked into' the AssemblyB's IL. This means that tomorrow if I update I_CONST_VALUE to 20 in the future. Assembly B would still have 2 till I recompile it.
  • in the case of the readonly value, it is like a ref to a memory location. The value is not baked into AssemblyB's IL. This means that if the memory location is updated, Assembly B gets the new value without recompilation. So if I_RO_VALUE is updated to 30, you only need to build AssemblyA. All clients do not need to be recompiled.

So if you are confident that the value of the constant won't change use a const.

public const int CM_IN_A_METER = 100;

But if you have a constant that may change (e.g. w.r.t. precision).. or when in doubt, use a readonly.

public readonly float PI = 3.14;
dipa ahuja replied to aman on 10-Sep-11 12:43 PM

Const: – The const keyword is used to modify a declaration of a field or local variable. It specifies that the value of the field or the local variable cannot be modified.



public const double x = 1.0;
public const int y=10;


ReadOnly: – The readonly keyword is a modifier that you can use on fields. When a field declaration includes a readonly modifier, assignments to the fields introduced by the declaration can only occur as part of the declaration or in a constructor in the same class.


Example: public readonly int y = 5;
public readonly string x;


Both Constant and ReadOnly keywords are used to deal with constant value but the difference in both is that the value of readonly variables is set at runtime, so it can have different value for different execution of the program. Whereas the value of constant variables is set during compile time.


Riley K replied to aman on 11-Sep-11 12:45 AM
const must be initialized at declaration time, readonly can be initialized on the constructor (and thus have a different value depending on the constructor used)

public class MyClass
  public const double PI = 3.14159;

PI cannot be changed in the application anywhere else in the code as this will cause a compiler error.

A constant member is defined at compile time and cannot be changed at runtime. Constants are declared as a field, using the constkeyword and must be initialized as they are declared. For example;

read only member is like a constant in that it represents an unchanging value. The difference is that a readonly member can be initialized at runtime, in a constructor as well being able to be initialized as they are declared. For example:

public class MyClass
  public readonly double PI = 3.14159;
public class MyClass
  public readonly double PI;
  public MyClass()
  PI = 3.14159;

Hope you got it