Keywords - Const, ReadOnly, And Static ReadOnly In C#

In this article, I am going to explain about Const, ReadOnly, & static ReadOnly keywords in C#.

Const keyword

It is used to declare a constant field and is also known as Compile-Time Constant. A constant field cannot be modified. So, if you want to assign a value to a variable that may be changed at any time in the future, please don't define it as constant. Constant variables, once declared, can never be changed further, i.e., neither in constructors nor in any method. Its value should only be assigned during its declaration time. It can only be used along with built-in C# types.

 

As from the above image, it can be seen that constant value once assigned cannot be changed in any of the constructors.

 

From the above image, it can be seen that constant is not accessible by object reference. It is directly accessible by using a class name. That means, const is by default static and it should not be used along with the static keyword. Below is the example of using const keyword.

Programming Example 1 - using const keyword
  1. using System;  
  2. namespace ConstStaticReadOnly {  
  3.     public class Constant {  
  4.         public  
  5.         const int iconstant = 2;  
  6.         public Constant() {  
  7.             //iconstant = 2; //The left-hand side of an assignment must be a variable, property or indexer ConstStaticReadOnly  
  8.         }  
  9.         static Constant() {  
  10.             //iconstant = 2; //The left-hand side of an assignment must be a variable, property or indexer ConstStaticReadOnly  
  11.         }  
  12.     }  
  13.     class Program {  
  14.         static void Main(string[] args) {  
  15.             Constant conObj = new Constant();  
  16.             Console.WriteLine(String.Format("Constant Value : { 0}", Constant.iconstant));  
  17.         }  
  18.     }  
  19. }  
Output

Constant Value: 2

ReadOnly Keyword

It is a modifier that can be applied to fields. Read-only is also known as Runtime constant because during runtime, its value can be changed. Value can be assigned either during the declaration of a variable or in constructors as shown below.
  • When the variable is initialized at the time of declaration:

  • For an instance variable field, it will be assigned only in instance constructor: 

  • For static variable field, it will be assigned only in a static constructor:

Programming Example 2 - using ReadOnly & Static ReadOnly keyword 
  1. using System;  
  2. namespace ConstStaticReadOnly {  
  3.     public class StaticReadOnly {  
  4.         public readonly int iReadOnly = 2;  
  5.         public static readonly int istaticReadOnly = 2;  
  6.         public StaticReadOnly() {  
  7.             iReadOnly = 10;  
  8.             //istaticReadOnly = 20; //compile time error as static variable value cannot be assigned in instance construtor  
  9.         }  
  10.         static StaticReadOnly() {  
  11.             istaticReadOnly = 20;  
  12.             //iReadOnly = 20; //compile time error as instance variable value cannot be assigned in static construtor, only static value should be assigned  
  13.         }  
  14.     }  
  15.     class Program {  
  16.         static void Main(string[] args) {  
  17.             StaticReadOnly stReadObj = new StaticReadOnly();  
  18.             Console.WriteLine(String.Format("ReadOnly Value : {0}", stReadObj.iReadOnly));  
  19.             Console.WriteLine(String.Format("Static ReadOnly Value : {0}", StaticReadOnly.istaticReadOnly));  
  20.         }  
  21.     }  
  22. }  
Output

ReadOnly Value: 10
Static ReadOnly Value: 20

Conclusion

I hope you enjoyed the article.

Thank you!


Similar Articles