Const, ReadOnly And Static Variables In C#

Const

 
Let's us look at an example.

Example 1 
  1. namespace Sample                                               {  
  2.     public class MyClass  
  3.     {  
  4.         private const int a;  
  5.   
  6.     }  
  7.     public class Sample2  
  8.     {  
  9.         static void Main()  
  10.         {          
  11.         }  
  12.     }  
  13. }  
Output 
 
Error 1 A const field requires a value to be provided.

A const variable must be initialized when declared and can't be modified later.

  1. Private static const int a=9; // Const can’t be marked as static  
Example 2
  1. namespace Sample  
  2. {  
  3.     public class Sample1  
  4.     {  
  5.         private const Sample2 sample2 = new Sample2();//Error  
  6.          
  7.     }  
  8.     public class Sample2  
  9.     {  
  10.       
  11.     }  
  12.     public class Sample3  
  13.     {  
  14.         static void Main()  
  15.         {      
  16.         }  
  17.     }  
  18. }  
Output 
 
Error 1. A const field of a reference type other than string can only be initialized with null.

A const field of a reference type other than string can only be initialized with null.

Example 3

  1. namespace Sample  
  2. {  
  3.     public class Sample1  
  4.     {  
  5.         private const Sample2 sample2 = null;  
  6.          
  7.     }  
  8.     public class Sample2  
  9.     {  
  10.       
  11.     }  
  12.     public class Sample3  
  13.     {  
  14.         static void Main()  
  15.         {    
  16.         }  
  17.     }  
  18. }  
Output 
 
No error

Example 4

  1. namespace Sample  
  2. {  
  3.     public class Sample1  
  4.     {  
  5.         public const int a = 1;  
  6.         public const int b = b + 2;  
  7.     }  
  8.     public class Sample3  
  9.     {  
  10.         static void Main()  
  11.         {             
  12.         }  
  13.     }  
  14. }  
The value of b evaluates at compile time, a constant can participate in a constant expression.

Static Field


Example 1
  1. namespace Sample  
  2. {  
  3.     public class Sample1  
  4.     {  
  5.         public static int c1;  
  6.         static void Main()  
  7.         {  
  8.             Console.WriteLine(c1);  
  9.             Console.ReadKey();  
  10.         }  
  11.     }  
  12. }  
Output 
 
0

Static variables are initialized as soon as the class loads with the default value. A variable in C# can never have an uninitialized value.

ReadOnly

  1. namespace Sample  
  2. {  
  3.     public class Sample1  
  4.     {  
  5.         public static readonly int a=8;  
  6.         static void Main()  
  7.         {  
  8.             a = 10;  
  9.             Console.WriteLine(a);  
  10.             Console.ReadKey();  
  11.         }  
  12.     }  
  13. }  
Output 
 
Error 1 A static readonly field cannot be assigned to (except in a static constructor or a variable initializer).

A Readonly field can be declared either when it is declared or in the constructor of its class.

Difference between const and Readonly keyword


A const field can only be initialized in the declaration of the field. A readonly field can be initialized either at the declaration or in a constructor. Therefore, readonly fields can have different values depending on the constructor used. A const field is a compile-time constant. A readonly field can be used for runtime constants.
  1. namespace Sample  
  2. {  
  3.     public class Sample1  
  4.     {  
  5.         public  readonly int _a; //but const field show error if field not assigned  
  6.         public Sample1(int a)  
  7.         {  
  8.             _a = a;  
  9.         }  
  10.         static void Main()  
  11.         {  
  12.             Sample1 sample = new Sample1(5);  
  13.               
  14.             Console.WriteLine(sample._a);  
  15.             Console.ReadKey();  
  16.         }  
  17.     }  
  18. }  
Output 
 
5