Why declare static classes as sealed and abstract in C#?

后端 未结 4 1319
你的背包
你的背包 2020-12-09 21:16

This MSDN article states that static classes should be declared as sealed and abstract. I was under the impression that static classes were already sealed. Why would you als

4条回答
  •  北海茫月
    2020-12-09 22:11

    • One of the effects of marking a class as abstract is that it cannot be instantiated.
    • One of the effects of marking a class as sealed is that is cannot be inherited.

    That's what a static class actually is -- a class that cannot be instantiated and that cannot be inherited.

    So the article doesn't state you should mark your static classes as abstract and sealed additionally, but this is the way how static classes are represented in IL.

    (Other languages may allow you to do this if they do not have a static keyword, although I doubt it, because an abstract sealed class doesn't make a lot of sense semantically.)

提交回复
热议问题