SIGN UP MEMBER LOGIN:    
ARTICLE

Rules for creating C# Static Classes

Posted by Man Mohan Sharma Articles | C# Language February 23, 2011
Here I show some rules to create a static class.
Reader Level:

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   

Login to add your contents and source code to this article
share this article :
post comment
 

thanks sir. Good work

Posted by sanjay gupta Mar 05, 2011

I have seen many other articles but this is good Man Mohan. It tells me when to use static. Good work!

Posted by Mahesh Chand Feb 24, 2011

thnx for providing such type of details.

Posted by Ankit Nandekar Feb 24, 2011

Cool! Thanks very much!

Posted by Kayleigh Feb 23, 2011
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Team Foundation Server Hosting
Become a Sponsor