Nishant Kumar Nishant Kumar - 1 year ago 65
C# Question

We can inherit abstract class but not static class

i don't know whether this question will make sense or a naive question, but i could not get the satisfactory answer till now. Any one who can redirect me the correct path.

When both can't be instantiated.

NOTE: if the question is clear kindly reduce the -ve repo to neutral or +ve

Answer Source

You're quite right, static class is abstract one:

  public static class MyStaticTest {


  // I'm abstract
  Console.WriteLine(typeof(MyStaticTest).IsAbstract ? "I'm abstract" : "");

and that's why you can't create an instance (compile time error): new MyStaticTest();. However, any static class is sealed one as well:

 // I'm sealed 
 Console.WriteLine(typeof(MyStaticTest).IsSealed ? "I'm sealed" : "");

and so you can't inherit from it (compile time error): public class MyClass: MyStaticTest {..}. Thus, all you can do with static class is to declare static members (static fields, properies, methods etc.)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download