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
- public class NormalClassA
- {
- }
- public static class StaticClassC : NormalClassA //Error -cannot inherit from NormalClassA class
- {
- }
- public static class StaticClassD : StaticClassC //Error -cannot inherit a static class as static class is sealed by //default
- {
- }
Below is error description.


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.
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.
- public static class StaticClassD
- {
- // All commented code below gives error on compile time and their reason is written against each of them
- //readonly int j; //Error - cannot declare an instance member in static class
- // int z; //Error - cannot have instance field outside static function or constructor
- readonly static int k; //default value is 0
- static int n; //default value is 0
- const int i = 90; //need to initialize at declaration and cannot change later
- //k++; //Error - readonly static can only change in static constructor
- //test() { } //Error - cannot have instance/non-static constructor
- static StaticClassD() { k++; n++; int data = 9; } //by default constructor is public //can use instance field insdie ctor
- //public void add() { } //Error - cannot have instance method inside static class
- public static void ChangedTextStaticMethod()
- {
- int d = 88;
- //k++; //Error - static readonly can only change in static constructor
- n++;
- Console.WriteLine("Value of static readonly variable k is :"+ k);
- Console.WriteLine("Value of static variable n is :" + n);
- Console.WriteLine("Value of constat variable i is :" + i);
- Console.WriteLine("Value of instance variable d is :" + d);
- }
- class Program
- {
- static void Main(string[] args)
- {
- StaticClassD.ChangedTextStaticMethod();
- }
- }
Below is 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.
- class NormalClassB
- {
- static NormalClassB()
- {
- Console.WriteLine("Static constructor of NormalClassB Called");
- }
- public NormalClassB()
- {
- Console.WriteLine("Non-Static constructor of NormalClassB Called");
- }
- }
- class NormalClassC :NormalClassB
- {
- static NormalClassC()
- {
- Console.WriteLine("Static constructor of NormalClassC Called");
- }
- public NormalClassC()
- {
- Console.WriteLine("Non-Static constructor of NormalClassC Called");
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- // static constructor will be called first - bottom to top
- // non-static constructor will be called after all static constructor - top to bottom
- NormalClassC normalClassC = new NormalClassC();
- }
- }
Below is output screen.



Hamid KhanPosted Mar 6, 2019, 5:28 AM
Good explanation Vikas Thanks for it...……. keep growing...….
Sagar Pandurang KapPosted Jan 2, 2018, 5:41 AM
Good going.Nice article.Well defined.