Constant Variable

Constant variable is a type of variable. It is declared and initialized the value at compile time. As name suggest, it is constant so value of the constant variable can’t be changed.

const keyword is used to declare the constant variable. By default constant variable is static, so you can’t define a constant type as static. Const keyword can apply to value type data type and reference type data type which can be assigned with a value null.

With Value Type 

const int x = 10;
const int y = 20;
const int z = x + y;
Console.WriteLine("The value of x :" + x);
Console.WriteLine("The value of y :" + y);
Console.WriteLine("The value of z :" + z);

//int a = 15;
//const int b = x + a;
//Console.WriteLine("The value of b :" + b); 
///	The variable 'a' is assigned but its value is never used

 

With Reference Type

const BaseClass objBase = null;
const BaseClass objBase1 = new BaseClass(); //It will throw an exception

'objBase1' is of type 'DemoCsharp.BaseClass'. A const field of a reference type other than string can only be initialized with null.

 

Read Only

It is declared and initialized the value at run time. The value of read only variable can be set/changed at the time of declaration or in the constructor of the class. readonly keyword is used to declare the read only variable. readonly keyword can apply to value type data type and reference type data type which initialized by using new keyword. You can’t make delegate and event as read only.

You can be declared readonly as static.

class Program
{
        readonly int x = 10;
        public Program()
        {
            //changed the value in constructor
            x = 20;
        }
}

Enjoy. Hope it will help you.