Read-Only Variables and Constant Variables in C#

 

Whenever we want declare variables with some data and that data never change again in application and no other developer should not change those variables data, we can use "read-only" or "constant" variables.

 

In this article we discuss about read-only variables and constant variables. It also covers difference between read-only and constant variables.

 

Read-Only Variables:

 

Read-Only variables declare by using "readonly" keyword. Once we declare the read-only variables we cannot change the value of those variables anywhere except in constructor. That means we can find readonly variable values only in constructor, other than constructor we cannot assign the value to readonly variables anywhere else as shown below.

 

namespace CSharpConstantReadOnly

{

    public class ReadOnlyClass

    {

        //declare readonly variable

        public readonly int iId;

        public ReadOnlyClass()

        {

            iId = 5;//No Error

        }

 

        iId = 6;//Compile time error       

    }

}

 

As shown above we can assign the value to readonly variables only while declaring the variables and in constructor. If you try to change the value of readonly variable other than at initial assignment and inconstructor, you will get errors as shown above.

 

ReadOnly variables are not static implicitly, so if you want to expose those variables outside the class we have to static keyword as shown below.

 

        //static read-only variable

        public static readonly int iEmpId = 10;

        iEmpId = 20;//Compile time error

 

We can access readonly variables as shown below.

 

using System;

namespace CSharpConstantReadOnly

{

    class Program

    {

        static void Main(string[] args)

        {

            Console.WriteLine(ReadOnlyClass.iEmpId);

            Console.ReadLine();

        }

    }

}

 

Constant Variables:

 

Like readonly variables, constant variables are used to store the constant data. But unlike readonly variables, once we declare the constant variables we cannot change the constant variable values anywhere else including constructor also.

 

We can declare constant variables by using "const" keyword. As shown below we can declare and initiate the constant variables by using const keyword. Once constant variables declared, we cannot change the value of the constant variables. That means constant variable value determined at compile-time only not at run-time.

 

namespace CSharpConstantReadOnly

{

    public class ConstantClass

    {

        //declaring constant variable

        public const int iId = 3;

        public ConstantClass()

        {

            iId = 5;//compile time error

        }

 

        iId = 5;//compile time error

    }

}

 

As shown above if you try to change the value of constant variable other than initial assignment you will get error including in constructor.

 

Unlike readonly variables, constant variables are implicitly static. So we can access constant variables as shown below.

 

           //access constant variables

            Console.WriteLine(ConstantClass.iId);

            Console.ReadLine();

 

 

Difference between Readonly and Constant variables

 

1)We can change the value of readonly variables in constructor, but we cannot change the value of the constant variables other than initial assignment while declaring.

 

2)ReadOnly variables are not static implicitly, but constant variables are implicitly static.

 

3)ReadOnly variables value determined at run-time only in constructor whereas constant variable values are not determined at run-time. Only compile time we can constant variable value.

                                                                                                                  

                                                                                           CSharpConstantReadOnly.zip (16.40 kb)