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

0 votes
61 views
Ethan Karla asked 27 days ago in programming by Ethan Karla

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

1 Answer

0 votes
Ravi Vishwakarma answered 6 days ago by Ravi Vishwakarma
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