What's the difference between an abstract class and a static one?

后端 未结 9 1916
臣服心动
臣服心动 2020-12-02 18:55

Neither is instantiable. What are the differences, and in what situations might you use one or the other?

9条回答
  •  Happy的楠姐
    2020-12-02 19:00

    Here is a short summary:

    • A static class can only contain static members (it is just a container for methods that do not logically belong to an instance of any standard class)
    • An abstract class can contain all usual kinds of members (static, abstract and also instance)

    The key difference is that you can inherit from an abstract class, but you cannot inherit from a static class. Technically speaking, the .NET runtime doesn't have any notion of static classes, so the C# compiler compiles them as classes that are both abstract and sealed (meaning that you cannot inherit from them).

    So, static classes are abstract classes that are also sealed (although this is not the usual way to look at the problem if you are C# programmer) and contain only static members (which is enforced by the C# compiler).

提交回复
热议问题