Difference between readonly and const

readonly and const keyword

In C#, readonly and const keywords are quite similar, both of them once declared, cannot be changed through out the entire application. However couple of differences are there for both the keywords.

Difference between readonly and const

constreadonly
A const variable has to be declared and initialized together.A readonly variable can be declared and initialized at different places. Typically it is declared as a class variable and initialized inside the constructor.
const variables are by default static. So, it should be accessed with the class name.readonly variables are not static by default. It should be accessed by object of the concerned class.
The value of a const variable is same for all the objects of a class, because it is a static variable.The value of a readonly variable varies from object to object. The reason because a readonly variable can be initialized inside constructor, so for different constructors, it can have different values.

Usability of the readonly and const

const is useful in a situation where the value of the variable remain same thorough out the application. Like the value of PI i.e. 3.141 is a constant. It cannot varies from object to object.

readonly is useful if the value varies from object to object. Like Database connection string. Based on the user input, we may have to save the data to different database. In this scenario, we can have several constructors of the same class with different DB connection strings.

Consider the below program to clear the concept. I recommend to run the below code and see how it is behaving.

using System;

namespace SimpleConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            SimpleTest test = new SimpleTest();
            //const variables are static by default
            Console.WriteLine("Constant Value: " + SimpleTest.myConstInt);
            Console.WriteLine("ReadOnly Value for default constructor: " + test.myReadOnlyInt);
            Console.WriteLine(test.myReadOnlyString);

            SimpleTest test1 = new SimpleTest(200);                        

            Console.WriteLine("ReadOnly Value for parameterized constructor: " + test1.myReadOnlyInt);
            Console.WriteLine(test1.myReadOnlyString);

            //const value can't be changed
            //SimpleTest.myConstInt = 100;
            //readonly value can't be changed
            //test.myReadOnlyInt = 100;

            Console.ReadKey();
        }
    }

    public class SimpleTest
    {
        //const variable has to be intialized
        //public const int myConstInt;

        public const int myConstInt = 10;
        
        //readonly variables can be initialized at declaration time or inside the constructor
        public readonly int myReadOnlyInt;
        public readonly string myReadOnlyString = "Readonly variable initialized at the time of Declaration.";

        public SimpleTest()
        {
            //const value can't be initialized inside constructor
            //myConstInt = 800;
            myReadOnlyInt = 100;
        }

        public SimpleTest(int readOnly)
        {
            myReadOnlyInt = readOnly;
            myReadOnlyString = "Readonly variable initialized inside the constructor.";
        }
    }
}

As a const variable is declared and initialized at the same time, so it’s value is available at the compile time. However, a readonly variable can be initialized at run time using different constructors. So, it’s value varies from object to object depending upon the constructor used to initialize the object.

Hope now reader is having a clear understanding of the difference between const and readonly keywords in C#.

Learn about yield keyword,
https://smoothknowledge.com/2018/02/06/yield-statement-in-c/

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.