Hi friends, In this tutorial, I am going to explain the difference between Constant and Readonly in C#. Constant and Readonly both are used to make field constant and their value cannot be modified. Here we will discuss the difference between these keywords.

Difference between Constant and Readonly in C#:

Constant:

Constants in C# are declared using the const keyword and must assign a value at the time of declaration. This value of const field can’t be changed throughout the program (means we cannot reassign a value in const field). The syntax for declaring a Constant in C# is:

Syntax:

access-modifier const data-type variable-name=value

Example:

class ConstantDemo
{
public const int iConstValue=3;
}

class Program
{
static void Main(string[] args)
{
Console.WriteLine(ConstantDemo.iConstValue);
Console.ReadLine();
}
}

We can not declare a constant variable as static. It is static implicitly. This means that we can a constant variable by using the class name.

The value in a const variable is what’s called a “compile-time” value, and is immutable (which means it does not change over the life of the program).

A Constant variable is declared only with the primitive data type like int, string, double, float etc. If we want to declare const for someclass (non-primitive types) we should assign it to null which as of no use.. Also, a constant variable is declared public, private, protected, internal, or protected internal access modifiers. Const field cannot be passed as ref or out parameter.

Key Point about Constant:

  • Constants are declared using const keyword in C#.
  • Constants must assign a value at the time of declaration.
  • The value of const field cannot be changed throughout the program.
  • We can not declare a const field as static. They are static implicitly.
  • the const field cannot be passed as refs or out parameter.

Readonly:

A readonly field can be declared using the readonly keyword. The readonly field can be initialized either at compile (at the time of declaration)or at the instance initialization (in the constructor only non-static). A readonly field can have different value depending upon the number of constructors used. So it may have different values depending upon the instance used. A readonly field can be used as runtime constant. Readonly is not static by default however we can declare a readonly field as static.

Example 1:

Initialize the readonly field at the time of declaration.

class ReadOnlyDemo
{
public readonly int iReadOnlyValue= 10;
}

class Program
{
static void Main(string[] args)
{
ReadOnlyEx readOnlyInstance = new ReadOnlyEx();
Console.WriteLine(readOnlyInstance.number);
}
}

Example 2:

Initialize the readonly field in constructor.

class ReadOnlyDemo
{
public readonly int iReadOnlyValue;

public ReadOnlyDemo()

{

iReadOnlyValue=1;

}
}

class Program
{
static void Main(string[] args)
{
ReadOnlyEx readOnlyInstance = new ReadOnlyEx();
Console.WriteLine(readOnlyInstance.number);
}
}

Key Point about readonly field:

  • Readonly fields are declare using readonly keyword.
  • Readonly fields are initialized at the time of declaration or in the constructor.
  • A readonly field can have different value depending on the constructor used.
  • A readonly field is not static in nature by default. Then can be declared as static expliciely.
  • A readonly field can be passed as ref or out parameter in constructor context.

 

 

Leave a Comment