2
What are the uses of const, readonly, static, and static readonly keywords in C#, and also differentiate with example?
What are the uses of const, readonly, static, and static readonly keywords in C#, and also differentiate with example?
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. |
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
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);
}
}
Constant : 3.14159
readonly : 11
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
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