Search code examples
c#static-classes

What is a "static" class?


In C# what is the difference between:

public static class ClassName {}

And:

public class ClassName {}

Solution

  • A static class cannot be instantiated, and can contain only static members. Hence, the calls for a static class are as: MyStaticClass.MyMethod(...) or MyStaticClass.MyConstant.

    A non static class can be instantiated and may contain non-static members (instance constructors, destructor, indexers). A non-static member of a non-static class is callable only through an object:

    MyNonStaticClass x = new MyNonStaticClass(...);
    x.MyNonStaticMethod(...);