const in C#

const keyword is used to create a field constant. The word const itself shows that it will never change. While doing the programming, and you know that some field’s value will never change, once declare then you can make those fields constant. As example the value of PI, distance between earth and moon, etc.

Don’t create a field as constant that you expect to change at any time.

You must provide initial values to const variable when they are declared. Therefore, a const variable is essentially a constant. Once you declared a field as constant, you cannot reassign its value.

You can declare constant numbers, Boolean values, strings, or a null reference.

Let us take an example to understand the const keyword in C#.

using System;
namespace ConsoleApplication1
{    
    class constDemo
    {
        public const  int  x = 25;
        // cannot mark as static, by default const is static
        //public static const int b=50;
    }
    class Program
    {               
        static void Main(string[] args)
        {                      
            const  int intVar=10;                  
            //Cannot reassigns the value.
            //Error:The left-hand side of an assignment must be a variable, property or indexer
            //intVar = 40;
            Console.WriteLine(intVar);
            // x variable is by default constant. Therefore called by the class name.
            Console.WriteLine("x = " + constDemo.x);
            // Error: The expression being assigned to 'value' must be constant
            // Here length is not constant.
            int length = 5;
            //const int value = 10 + length;
            Console.ReadLine();
        }
    }
}


In the above code, constant x is declared in constDemo class as it is by default static. Due to its static nature it will be called by class name.

Constant intVar is declared in Program class and if you try to reassigns the value it will give compile time error.

Again if you try to write the code as
const int value = 10 + length;
It will also give the error, because length variable is not declared as constant.

readonly

readonly constant is like const but readonly variables can be assigned at the time of declaration or can be assigned value inside a constructor. const field is a compile-time constant but the readonly field is runtime constants.

using System;
namespace ConsoleApplication1
{
    class MyClass
    {
        readonly int intVar = 25; // initialized at the time of declaration
        readonly int readOnlyVar;       
        public MyClass(int x)
        {
            readOnlyVar = x; // initialized at run time
            Console.WriteLine("readOnlyVar = " + readOnlyVar);
        }
        public void SetData()
        {
            // It will give the compile time error.
            // You cannot set the value in function.
            //readOnlyVar = 100;
            // intVar has already initialized.
            Console.WriteLine("intVar = " + intVar);
        }
    }    
    class Program
    {              
         static void Main(string[] args)
         {
             MyClass obj = new MyClass(30);
             obj.SetData();
             Console.ReadLine();
         }
    }
}

Difference between const and readonly

constreadonly
Must be initialized at the time of declaration of the field.Initialized at the time of declaration or the constructor of the class
By default const fields are staticBy default readonly fields are not static. It can use with static modifier.
You cannot reassign a const variable.You can reassign a const variable in constructor.
It is called as compile time constantIt is called as run time constant
Can be declared in function.Cannot declare in function.