Creating static class

I am trying to create a static class but am getting a compile error.

public static class myClass

{

}

gives compile error "The modifier 'static' is not valid for this item"

According to MSDN documetation, the above code should create a static class.

How do I create a static class

Thanks in advance.



Answer this question

Creating static class

  • newport27

    What .NET/Visual Studio version are you using

    static classes were added in .NET2.0/VS2005.


  • Mike Epprecht - SQL MVP

    In .NET 1.x , you cannot add the keyword 'static' at class level.

    If you want to create a class that is only going to have static members in .NET 1.x, then, you can create the class 'sealed' (so that no one can override it), and give it a private default constructor.
    In this way, you will not be able to create instances for this class:

    public sealed MyStaticClass
    {
    private MyStaticClass(){}

    public static void MyStaticMethod()
    {
    }
    }


  • rebeccat

    What version of the C# compiler are you using

    The static modifier is new for 2.0 (Visual Studio 2005).



  • Creating static class