Why are static classes closed?

I understand what static classes and private classes are, and I regularly use extension methods, I'm just wondering: does anyone know why static classes are sealed in C #?

I looked at the MSDN and the C # language specification, but it never says why they are sealed.

Why can't we inherit from static classes and redefine static members, etc.?

Edit:

I appreciate your answers, but you still talk about what a static class is. I know why I cannot override its methods. But I ask, why did they do this?

Are vtables really expensive? Why is langauge design so static classes are literally static? Is it just for tradition? Is there another advantage that I do not see?

(I have a suspicious suspicion that I fundamentally misunderstand the point of static classes.)

+5
source share
2 answers

You cannot inherit static classes because you cannot override static members. You cannot redefine static members because the whole concept of redefining elements depends on virtual sending to the type of an implicit parameter, and static members do not have an implicit parameter to send.

+6
source

Sealing a class means you cannot use it as a superclass. Creating a static class makes them useless as base classes because they cannot have overridable methods. Therefore, the conclusion from a static class is of dubious significance: it can be argued that you could share protected methods from a static base, but then it is a β€œone-way street” because derived classes cannot change the functionality of their databases, providing useful overrides.

This negates the utility of static classes to a repository of methods similar to a namespace and the associated static data. Giving such classes inheritance of other static classes will make this task less clear, adding very little functionality or convenience.

+4
source

Source: https://habr.com/ru/post/1205912/


All Articles