Difference between Read-only and Constant in C#

Constant

Const is nothing but “Constant”.

A variable declared as “const” must be assigned a value at declaration, and this value may not then change at a later time.

The Value in a const variable is what is called a “compile-time” value and is immutable.

You can apply const keyword to built-in value type(byte, short, int, long, char, float, double, decimal, bool), enum a string literal, or a reference type which can be assigned with a value null.

Constants can be marked as public, private, protected, internal, or protected internal access modifiers.

Constant the following example :

using System;

namespace ConstandReadonlyExample
{
    class ConstantExample
    {
        const string alpha = "Constant Example";
        public ConstantExample()
        {
            alpha = "Constant Example in c#";
        }
        void Super()
        {
            alpha = "Constant";
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

ReadOnly 

Readonly is the keyword whose value we can change during runtime or we can assign it at run time but only through the non-static constructor. Not even a method.

Readonly the following example :

using System;

namespace ConstandReadonlyExample
{
    class ConstantExample
    {
        readonly String Example;
        public Demo()
        {
            Example = "Readonly Example";
            Console.WriteLine(Example);
        }
        public Demo(string value)
        {
            Example = value + "Readonly";
            Console.WriteLine(Example);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Demo d = new Demo();
            Demo demo = new Demo("Readonly Example");
            Console.ReadKey();
        }
    }
}

Difference between Readonly and Constant

Constant Readonly
The Constants are evaluated at compile time. The Read-Only variables are evaluated at run time.
It’s mandatory to assign a value at the time of declaration.
eg,
int const a=10;
It’s not mandatory to assign a value at the time of declaration.
eg,
int read-only a;
a=0;
Assigned values of constant variables can not be changed. Read-only variables values can be changed even after assigning.
The constant can be declared at the class level as well as inside the method level. Readonly can be declared only at a class level, not inside the method.
Constants in C# provide a named location in memory to store a data value. It means that the value of the variable will be known in compile-time and will be stored in a single place. The read-only keyword is a modifier that can be used in four contexts: In a field declaration, read-only indicates that assignment to the field can only occur as part of the declaration or in a constructor in the same class.

We are known for our ability to create web apps that are lightweight, stable, future-ready, custom-made, and scalable. It doesn’t matter if it’s a small website or a complex online application. Hire .Net developers in India from vision infotech to bring ease to your web application development work. Our .NET developers are capable of working on large-scale mobile app development projects. Dedicated ASP.NET Developers & Programmers who are Xamarin experts to help you design hybrid mobile apps for both iOS and Android in less time.

Submit a Comment

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

Subscribe

Select Categories