1

Why can't a static class be inherited into a normal class?

3
  • 3
    Question makes no sense. There would be no benefit from doing so. Commented Dec 23, 2009 at 5:54
  • 2
    +1 I think it's a good question - one I hadn't thought about. If there is no perceived benefit of doing something, should a language STOP me from doing it? I don't think so. Commented Dec 23, 2009 at 6:03
  • duplicate of stackoverflow.com/questions/774181/… Commented Dec 23, 2009 at 6:26

5 Answers 5

4

If B inherits from (is a subclass of) A, that means an instance of B can be stored in a variable of type A, and its virtual methods will call those of class B.

For static classes, you don't have the concept of an instance of the class, so there's no way to inherit. You might have better luck with a static (singleton) reference to a regular class.

Sign up to request clarification or add additional context in comments.

Comments

2

From Static Classes and Static Class Members (C# Programming Guide)

Creating a static class is therefore basically the same as creating a class that contains only static members and a private constructor. A private constructor prevents the class from being instantiated. The advantage of using a static class is that the compiler can check to make sure that no instance members are accidentally added. The compiler will guarantee that instances of this class cannot be created.

Static classes are sealed and therefore cannot be inherited. They cannot inherit from any class except Object. Static classes cannot contain an instance constructor; however, they can contain a static constructor.

Comments

1

As an alternative to inheriting from a static class, you can assign extension methods to interfaces.

1 Comment

helper objects as.. public static <class> function(this <class> ss)
1

You can not inherit a static class - The reason is simple. Static classes are marked as abstract and sealed in compiled IL which can be neither instantiated nor inherited.

Comments

-1

This is actually by design. There seems to be no good reason to inherit a static class. It has public static members that you can always access via the class name itself. The only reasons I have seen for inheriting static stuff have been bad ones, such as saving a couple of characters of typing.

There may be reason to consider mechanisms to bring static members directly into scope (and we will in fact consider this after the Orcas product cycle), but static class inheritance is not the way to go: It is the wrong mechanism to use, and works only for static members that happen to reside in a static class.

(Mads Torgersen, C# Language PM)

Source: Why can't I inherit static classes?

1 Comment

Please mention that there's an exact SO duplicate to the question instead of copying the winning answer from the duplicate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.