7

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

I've looked at MSDN and the C# language specification, but it never really says why they're sealed.

Why shouldn't we be able to inherit from static classes and override static members, etc.?

Edit:

I appreciate your answers, but you're still talking about what a static class is. I know why I can't override its methods. But I'm asking why did they make it that way?

Are vtables really that expensive? Why design a langauge so static classes are literally static? Is it just for tradition? Is there another advantage I'm not seeing?

(I have the sneaking suspicion I'm fundamentally misunderstanding the point of static classes.)

Sam Porch
  • 751
  • 5
  • 12
  • You can't override any static member, even if the class wasn't static; you can only ever shadow them... Why not use a design pattern like singleton instead, where you override the members (because it's a static instance, not class) or other design patterns? – Brian Mains Oct 31 '14 at 02:05
  • @BrianMains It's not a singleton, it's a zeroton. There are never any instances, rather than exactly one. – Servy Oct 31 '14 at 02:06
  • 1
    @Servy I believe Brian is suggesting that the OP use a singleton, not that a static class is one. – DavidG Oct 31 '14 at 02:07
  • 1
    It doesn't make sense to inherit from a static class. There are no instances; you don't need dynamic dispatch. You must know at compile-time which method you want to call. – Blorgbeard Oct 31 '14 at 02:08
  • This [Why static classes declared sealed](http://stackoverflow.com/questions/1268983/why-declare-static-classes-as-sealed-and-abstract-in-c) explains why... But your question seem to be more on why inheritance not allowed, so not exactly duplicate. – Alexei Levenkov Oct 31 '14 at 02:09
  • @BrianMains, you're right, now that I take a closer look at Singleton that's probably what I'm after—a static object rather than a static class. – Sam Porch Nov 05 '14 at 22:55

2 Answers2

9

You can't inherit static classes because you can't override static members. You can't override static members because the entire concept of overriding members is dependent on virtual dispatch on the type of the implicit parameter, and static members have no implicit parameter to dispatch on.

Servy
  • 202,030
  • 26
  • 332
  • 449
  • 1
    could you elaborate on "virtual dispatch"? I could google without looking ignorant but just wondering. – RadioSpace Oct 31 '14 at 02:09
  • @RadioSpace It's a pretty complex subject. Just google it if you want to know more. It's well beyond the scope of an SO answer. – Servy Oct 31 '14 at 02:10
  • good enough for me. thanks. I mostly browse for terms to google anyways. – RadioSpace Oct 31 '14 at 02:10
  • Yes, I agree that's the literal answer. But, I'm asking a broader question—do you know why they made it that way? Why not include vtables in a static class? Is there some advantage I'm missing? – Sam Porch Oct 31 '14 at 21:32
  • @SamPorch What are you expecting them to dispatch *on*? Instance methods are using the type of their implicit parameter as the key in that lookup table. Static methods have no such parameter, so what would you expect them to use as the key? – Servy Nov 03 '14 at 14:48
  • @Servy, yes, I see what you mean now, static classes _must_ be sealed because there is no instance on which to base a vtable. Thanks very much. – Sam Porch Nov 05 '14 at 22:59
5

Sealing a class means that you cannot use it as a superclass. Making a class static makes them useless as base classes, because they cannot have overridable methods. Therefore, deriving from a static class has questionable value: one could argue that you could share protected methods from a static base, but then it is a "one-way street", because derived classes cannot alter functionality of their bases by providing useful overrides.

This boils down the utility of static classes to a namespace-like holder of methods and their associated static data. Letting such classes inherit other static classes would make this purpose less clear while adding very little in terms of functionality or convenience.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523