Introduction : In this article, let us understand the basic differences between the const and the readonly keywords and also understand how to use them in our code.
1) Const by default static but Readonly not.
const int k = 6; // Constant by default is static and can only be called with class name eg: ConstReadonly.k
readonly int r = 8; // Readonly by default is non static and can be called with object name.
2) Const can be local Variables but Readonly cannot be local Variables.
public void Method1()
const int c = 1; // No Issue const can be local variable
readonly int r=6; // Compiler Error readonly can not be local variable.
3) Const must be initialized as compile time. But Readonly can be assigned at Compile time or within Constructor.
const int i = 7; // Constant must be initialized at compile time
readonly int k ;
public ConstReadonly()// Readonly can be initialized at compile time or within constructor
k = 9;
4) We can assign Readonly Variable with any Variable of same type, but Const Variable can be assigned with only another Const Variable of same type.
int k = 9;
const int i = k;// Compiler Error
const int j = i;// No Issue
readonly int m = k; // No Issue
5) Const should be used only with value type exclude String. Readonly can be used with any type
6) If we have declared global Const and never used in application complier will not give warning but for Readonly compiler will give warning.
Thanks for reading article . for any query send mail to