What is a “static” class?

后端 未结 10 1427
面向向阳花
面向向阳花 2020-12-04 16:35

In C# what is the difference between:

public static class ClassName {}

And:

public class ClassName {}
10条回答
  •  攒了一身酷
    2020-12-04 17:15

    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(...);
    

提交回复
热议问题