C# - Difference Between Const, ReadOnly and Static


There are many different keywords available in C#. This tutorial will look at the differences between the keywords readonly and const keywords in C# and when to use each.

Difference Between Const, readOnly and Static in C#

1. Const in C#

By default, a const is static. In C#, a constant is a value that cannot be changed during the execution or run time of a program. That means value of Const will not change during whole program. It's mandatory to assign a value to it when we declared it. Using constants can improve code readability and maintainability, and can also help prevent bugs caused by accidental modification of values. Here, you will see how to declare and use a constant in C#.

Points to Remember

  1. It is a Compile-time constant.
  2. It can't be declared static.
  3. It can't be modified or changed.
  4. It can be of any type of Access Modifier.
  5. it needs to get initialized.
  6. It is declared at the time of declaration.

Example

This will declare the variable as "constant", which means unchangeable and read-only.

using System;
using System.Text;

namespace FirstProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            const double pi = 3.14159;   // constant declaration That means value of that will not change.
            double r;
            r = Convert.ToDouble(Console.ReadLine());
            double area = pi * r * r;           
            Console.WriteLine("Area of circle is: {0}", area);            
        }        
    }
}

Output

Difference Between Const, readOnly and Static in C#

2. Readonly in C#

A Readonly field can be initialized either at the time of declaration or within the constructor of the same class. We can also change the value of a Readonly at runtime or assign a value to it at runtime (but in a non-static constructor only).

Points to Remember

  1. It is a run-time constant.
  2. It can be static.
  3. It can be declared in the constructer class.
  4. It is defined generally public.

Example

using System;
using System.Text;

namespace FirstProgram
{
   public class TestClass
    {
        public readonly string ConnectionString = "TutorialsTrend";

        public TestClass()
        {
            ConnectionString = "Differentsites"; // declared in the constructer class will compile
        }

        public void TestMethod()
        {
            ConnectionString = "NewSites";//Will not compile
        }
    }
}

Error on Test Method

Difference Between Const, readOnly and Static in C#

3. Static in C#

The static keyword is used to declare a static member. If we are declare a class as a static class then in this case all the class members must be static. The static keyword can be used effectively with classes, fields, operators, events, methods etc.

Points to Remember

  1. It can't be used with indexers.
  2. It can work with constructors.
  3. It is used by default as private.
  4. It can be parameterized or public.
  5. If its applied to a class then all the class members need to be static.

Example

using System;
using System.Text;

namespace FirstProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            Program.MyMethod(); // Working Fine and compile code
            
            Program pt= new Program();
            pt.MyMethod(); //Will not compile
        }

        public static void MyMethod()
        {
            Console.WriteLine("TutorialsTrend.Com");
        }
    }    
}

Compiler will not compile that code.

Program pt= new Program();
pt.MyMethod(); //Will not compile

Difference Between Const, readOnly and Static in C#

Difference between Const, ReadOnly and Static

S.No. Const ReadOnly Static
1 A Const can only initialized at the time of declaration. A ReadOnly can be initialized at the time of declaration or within the constructor of the same class. A Static value can ve assigned at run time or compiled time and may be change at run time.
2 A Const field is a compile time constant. A ReadOnly field is used as a run time constant. A static field is used as a run time constant.
3 It can be access using classname.constname It can be access using Instance It can be access using classname.constname
4 Once a value is assigned to a constant. It can not be initialized again. You can change ReadOnly value number of times. You can also change it inside constructor. Static value can only be changed inside static constructor only. It can be changed only onece at run time.

Prev Next

Top Articles

  1. Why use C#
  2. How to use comment in C#
  3. How to use variables in C#
  4. How to use keywords in C#