What are the uses of const, readonly, static, and static readonly keywords in C#, and also differentiate with example?

Asked 25-Aug-2021
Viewed 536 times

2

What are the uses of const, readonly, static, and static readonly keywords in C#, and also differentiate with example?


2 Answers


1

Difference between static, readonly, and constant in C#

The following table lists the difference between Static, Readonly, and constant in C#.

static readonly const
Declared using the static keyword. Declared using the readonly keyword. Declred using the const keyword. By default a const is static that cannot be changed.
Classes, constructors, methods, variables, properties, event and operators can be static. The struct, indexers, enum, destructors, or finalizers cannot be static. Only the class level fields can be readonly. The local variables of methods cannot be readonly. Only the class level fields or variables can be constant.
Static members can only be accessed within the static methods. The non-static methods cannot access static members. Readonly fields can be initialized at declaration or in the constructor.

Therefore, readonly variables are used for the run-time constants.
The constant fields must be initialized at the time of declaration.

Therefore, const variables are used for compile-time constants.
Value of the static members can be modified using ClassName.StaticMemberName. Readonly variable cannot be modified at run-time. It can only be initialized or changed in the constructor. Constant variables cannot be modified after declaration.
Static members can be accessed using ClassName.StaticMemberName, but cannot be accessed using object. Readonly members can be accessed using object, but not ClassName.ReadOnlyVariableName. Const members can be accessed using ClassName.ConstVariableName, but cannot be accessed using object.


 


2

Const keyword
The const keyword in C# is used when we want to make constant variable in program, the word const also tells the C# compiler that the value of the constant we can make any type of constant value.
Syntax 

const datatype variable_name [ UpperCase ] = value; // value assign must be mandatory because const value must assign at compile time not at run time.
const float PERCENT = 6.0;
const float PI = 3.14159
readonly
readonly keyword is used to define a variable which can be assigned value in variable, once after declaration either during declaration or in constructor.
Syntax

 readonly datatype variable_name ; // its declaration of readonly variable, we can assign value at run time in constructor

readonly  const
readonly is a constant defined at runtime or compile time. const is used to create a constant variable at compile time.
readonly field variables value can be changed after declaration.  const field value initlize at declaration time.
readonly fields cannot be defined within a method.  but const fields can not be declared within a method.


using System.IO;
using System;
public class Program {
   public const int PI = 3.14159;
   public readonly int value1;
   public Program(int value){
      value1 = value;
   }
   public static void Main() {
      Console.WriteLine(“Constant : “+PI);
      Program p1 = new Program(11);
      Console.WriteLine(“readonly : “+p1.value1);
   }
}
Output
Constant : 3.14159

readonly : 11

Static keyword
Instance Variables of a Class which are declared static, they behave like Global Variables in a way, so when Objects of that Class are created, then copy of all Non-Static Data Members for each Object So it is created, but the copy of static data members is not created. That is, the static data member is commonly used for all the objects of that entire class and all the objects of that class share the static data member equally. So now if we want to know how many objects of Box class we have created till the current statement, then we can modify our previous program as follows
Syntax 

 static datatype variable_name = [ initlize ] ;
using System;

public class StaticDemo
{
 private static int count=0;
 private string name;
 public StaticDemo()
 {
  this.name=string.Empty;
  count++;
 }
 public StaticDemo(string Name)
 {
  this.name=Name;
  count++;
 }
 public void CountNoOfObject()
 {
  Console.WriteLine('Number of object is : '+count);
 }
}
public class Program
{
 public static void Main()
 {
  StaticDemo sd1=new StaticDemo();
  StaticDemo sd2=new StaticDemo('mindstick');
  StaticDemo sd3=new StaticDemo();
  StaticDemo sd4=new StaticDemo();
  sd3.CountNoOfObject();
 }
}
Output

 Number of object is : 4

Static readonly
 It variable is same as the readonly variable, readonly variable can assign value in constructor not in static constructor. So if we use the static readonly then we assign value in static constructor, then assign value in static readonly variable.
Syntax

 static readonly datatype variable_name;
using System;

public class StaticDemo
{
 private static readonly int count;
 private string name;
 static StaticDemo()
 {
  count++;
 }
 public StaticDemo()
 {
  this.name=string.Empty;
 }
 public StaticDemo(string Name)
 {
  this.name=Name;
 }
 public void CountNoOfObject()
 {
  Console.WriteLine('Number of object is :'+count);
 }
}
public class Program
{
 public static void Main()
 {
  StaticDemo sd1=new StaticDemo();
  StaticDemo sd2=new StaticDemo('mindstick');
  StaticDemo sd3=new StaticDemo();
  StaticDemo sd4=new StaticDemo();
  sd3.CountNoOfObject();
 }
}
Output

 Number of object is :1