Rules for creating C# Static Classes


The following are rules to create a static class:

Rule#1.  a static class must contain the static keyword in the class declaration.

Rule#2.  a method should be static i.e. the method declaration must contain the static keyword.

Rule#3.  the Constructor must be static because a static class doesn't allow an instance constructor.

Rule#4.  the Constructor must not contain an access modifier.

Rule#5.  a Static class can't implement an inteface.

Rule#6.  a Static class can't be a base class i.e. it cannot be derived from.

public static class clsStatic
{
    static string varA = string.Empty;
    static int varB = 0;
    static clsStatic()
    {
        varA = "Give some value";
        varB = 100;
    }
    public static void MethodA()
    {
    }
    public static void MethodB()
    {
    }
}

Experiment #1:

Let's do an experiement; try to violater some rules i.e. Rule #1,2,3.

Remove the static keyword from the Constructor, member variable and also from methods.

public static class clsStatic
{
    string varA = string.Empty;
    int varB = 0;
    public  clsStatic()
    {
        varA = "Give some value";
        varB = 100;
    }
    public void MethodA()
    {
    }
    public void MethodB()
    {
    }
}

C# Comiler will give you the following error:

Error   1        Static classes cannot have instance constructors
Error   2        'MethodA': cannot declare instance members in a static class
Error   3        'MethodB': cannot declare instance members in a static class
Error   4        'prjStatic.clsStatic.varA': cannot declare instance members in a static class
Error   5        'prjStatic.clsStatic.varB': cannot declare instance members in a static class

Experiement #2:

Let's try another experiement; let's change the access modifier of the constructor; we are breaking Rule #4.

public static class clsStatic
{
    static string varA = string.Empty;
    static int varB = 0;
    //Break the Rule # 4, declare constructor as public
    public static clsStatic()
    {
        varA = "Give some value";
        varB = 100;
    }
    public static void MethodA()
    {
    }
    public static void MethodB()
    {
     }
}

C# Compiler will give you the following error.

Error     1          'prjStatic.clsStatic.clsStatic()': access modifiers are not allowed on static constructors

So, what is the reason behind that we can't put access modifier in static constructor?

CLR controls the static class and invoke the static constructor, so static constructor does not require any access modifier.

Experiment #3:

Let's try to implement an interface in a static class:

interface iStaticCls
{
     void MethodC();
     void MethodD();
}
public static class clsStatic : iStaticCls
{
    public static void MethodA()
    {
    }
    public static void MethodB()
    {
    }
}

Try to rebuild the code, you will get the following error:

Error     1          'prjStatic.iStaticCls': static classes cannot implement interfaces   

Up Next
    Ebook Download
    View all
    Learn
    View all