All About Static Method, Constructor And Class In C#

A static class cannot be derived from a non-static as well as static class. Static classes are by default sealed so you cannot inherit them. It can only inherit the Object class.
 
Code Snippet
  1. public class NormalClassA  
  2. {  
  3. }  
  4. public static class StaticClassC : NormalClassA //Error -cannot inherit from NormalClassA class  
  5. {  
  6. }  
  7. public static class StaticClassD : StaticClassC  //Error -cannot inherit a static class as static class is sealed by //default  
  8. {  
  9. }  
Below is error description.

error
 
You cannot declare an instance field inside static class(outside non-static method and constructor).But you can use an instance variable inside static constructor and static method.

You can declare a read-only static variable in static class but it can only be changed in a non-static constructor. But you cannot change the read-only static variable from inside a static method.

You can declare and initialize a constant variable inside static class once and thereafter it can't be changed.

You cannot have non-static constructor inside a static class. Since we can't create an object of the static class to invoke it.

You can declare a static variable at first inside the static class which can be later initialized/changed from inside a constructor and/or from a static method.

Below is code snippet.
  1. public static class StaticClassD  
  2. {  
  3.    // All commented code below gives error on compile time and their reason is written against each of them  
  4.    //readonly int j; //Error - cannot declare an instance member in static class  
  5.    // int z; //Error - cannot have instance field outside static function or constructor  
  6.    
  7.             readonly static int k; //default value is 0  
  8.             static int n;               //default value is 0  
  9.             const int i = 90;        //need to initialize at declaration and cannot change later  
  10.    
  11.    //k++; //Error - readonly static can only change in static constructor  
  12.    //test() { } //Error - cannot have instance/non-static constructor  
  13.    
  14.             static StaticClassD() { k++; n++; int data = 9; } //by default constructor is public //can use instance field insdie ctor  
  15.    
  16.    //public void add() { } //Error - cannot have instance method inside static class  
  17.    
  18. public static void ChangedTextStaticMethod()  
  19. {  
  20.             int d = 88;  
  21.    //k++; //Error - static readonly can only change in static constructor  
  22.             n++;  
  23.             Console.WriteLine("Value of static readonly variable k is :"+ k);  
  24.             Console.WriteLine("Value of static variable n is :" + n);  
  25.             Console.WriteLine("Value of constat variable i is :" + i);  
  26.             Console.WriteLine("Value of instance variable d is :" + d);  
  27. }  
  28.    
  29. class Program  
  30. {  
  31.       static void Main(string[] args)  
  32.          {  
  33.             StaticClassD.ChangedTextStaticMethod();  
  34.          }  
  35. }  
Below is output.

output
 
Static default constructor execution order is from bottom to top (child to parent). And, Non-Static default constructor execution order is from top to bottom (parent to child).
 
Below is the code snippet.
  1. class NormalClassB  
  2. {  
  3.             static NormalClassB()  
  4.                {      
  5. Console.WriteLine("Static constructor of NormalClassB Called");      
  6.                }  
  7.             public NormalClassB()  
  8.               {  
  9. Console.WriteLine("Non-Static constructor of NormalClassB Called");  
  10.                }  
  11. }  
  12.    
  13. class NormalClassC :NormalClassB  
  14. {  
  15.          static NormalClassC()  
  16.             {  
  17.          Console.WriteLine("Static constructor of NormalClassC Called");  
  18.             }  
  19.          public NormalClassC()  
  20.             {  
  21. Console.WriteLine("Non-Static constructor of NormalClassC Called");  
  22.             }  
  23. }  
  24.    
  25. class Program  
  26. {  
  27. static void Main(string[] args)  
  28.          {  
  29.       // static constructor will be called first - bottom to top  
  30.       // non-static constructor will be called after all static constructor - top to bottom  
  31.                NormalClassC normalClassC = new NormalClassC();  
  32.          }  
  33. }  
Below is output screen.

output
X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now