Coder Perfect

Isn’t it true that I can’t inherit static classes?


There are a few classes in my library that don’t require any state. I’d like to put them in a hierarchy from an organizational standpoint.

However, it appears that I am unable to specify inheritance for static classes.

Something like that:

public static class Base

public static class Inherited : Base

will not work.

Why did the language’s inventors rule out such possibility?

Asked by User

Solution #1

Citation from here:

Other Channel 9 viewpoints

Littleguru provides a partial “workaround” for this issue: the Singleton pattern, which is a great idea.

Answered by boj

Solution #2

The fact that static classes are abstract and sealed is the fundamental reason why you can’t inherit them (this also prevents any instance of them from being created).

So this:

static class Foo { }

compiles to the following IL:

.class private abstract auto ansi sealed beforefieldinit Foo
  extends [mscorlib]System.Object

Answered by Andrew Hare

Solution #3

Consider the following scenario: Static members are accessed by type name, such as this:


If you wanted to inherit from that class, you’d have to use the new type name:


As a result, when used in code, the new item has no resemblance to the old. For things like polymorphism, there would be no way to take advantage of any inheritance link.

Maybe all you want to do is expand on some of the items in the original class. There’s nothing stopping you from using a member of the original type in a whole new type in that situation.

Perhaps you wish to add methods to a static type that already exists. You can already do this with extension methods.

You might want to be able to give a static Type to a function at runtime and call a method on that type without knowing what it does. You can utilize an Interface in that instance.

Inheriting static classes does not provide any benefit in the end.

Answered by Joel Coehoorn

Solution #4

Hmmm… how different would it be if you just have non-static classes with static methods?

Answered by CookieOfFortune

Solution #5

What you want to accomplish using class hierarchy may be accomplished simply by using namespacing. As a result, languages that enable namespacing (such as C#) will have no need to construct static class hierarchy. Because none of the classes can be instantiated, all you need is a hierarchical organization of class declarations, which you can get by using namespaces.

Answered by Amit

Post is based on