Understanding Constant and Read-Only Variables in C#

 

Generally we create the variables and we change the values several times within our application based on requirements. But in some situations we have a requirement that where we have to maintain the variable values as fixed. For this type of requirements we can use const and readonly variables.

 

Understanding the Constant variables:

We can create constant variables by using const keyword in C#. For constant variables we have to assign the value at the time initialization. You cannot change the value of constant variables after initialization, if you try to change the value of constant variables after declaration you will get the compile time errors. This is because value of constant variables must be known at compile time only.

 

For example we have an employee class Employee1.cs which has a constant variable id as shown below. If we try to change the value of constant variable id after declaration you will get the compile-time error as shown below.

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace ConstReadOnlyExp

{

    class Employee1

    {

         constint id = 10;

 

        Employee1()

        {

             //compile time error

              id = 100;

        }

 

       private void display()

        {

            //compile time error

            id = 200;

        }

    }

}

 

You can access the constant variables same as normal variables depending on their scope. Constant fields are implicitly static.

 

Understanding the Read-Only Variables:

 Read-Only variables are same as constant variables. Like Constant variables, you cannot change the read-only variables values after initial assignment but you can change the read-only variables in constructor. The value of read-only variables can be changed at runtime in constructor.

 

Unlike constant variables, you will not get any compile-time errors even if you do not assign any value to read-only variables while initialization because read-only variable values determined ate run-time.

 

Same as constant variables you cannot change the read-only variables other than in constructors. If you try to change the value of read-only variables other than in constructor, you will get the compile time errors as shown below.

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace ConstReadOnlyExp

{

         class Employee2

         {

             readonly int id = 10;

 

             Employee2()

             {

                 //no compile time error

                 id = 100;

             }

 

             private void display()

            {

                  //compile time error

                   id = 200;

             }

    }

}

 

As shown above, if you try to change the value of read-only variable in constructor you will not get any errors. But other than in constructor, if you try to change the read only variables you will get compile-time errors.

 

Read-Only variables cannot be static implicitly. To make read-only variable as static you have to declare read-only variable as static explicitly as shown below. You can changed the value of static read-only variables in static constructor as shown below.

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace ConstReadOnlyExp

{

       class Employee3

      {

            static readonlyint id = 10;

 

           static Employee3()

          {

               //no compile time error

                id = 100;

         }

 

         private void display()

        {

               //compile time error

              id = 200;

        }

    }

}

 

The main difference between constant and read-only variable, you cannot change the constant variable value other than initialization (that means other than compile-time you cannot assign value to constant variables) and you can change the read-only variables in class constructor(that means you can change the value of read-only variables at run-time).

                                                                                                       ConstReadOnlyExp.zip (15.08 kb)