5

Jon Skeet, in his book C# in Depth, says about a static class:

It can't be declared as abstract or sealed, although it's implicitly both.

An abstract class is meant to be a base class for derived types. We can instantiate an abstract class only by instantiating one of its derived types. On the other hand, we cannot derive anything from a sealed class. A sealed, abstract class would be useless in many senses. What does Skeet mean by a static class being both abstract and sealed? Is he just talking about the inability to instantiate it directly?

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
Shaun Luttin
  • 133,272
  • 81
  • 405
  • 467

4 Answers4

20

What does Skeet mean by a static class being both abstract and sealed?

I mean that that's the representation in the IL.

For example:

static class Foo {}

Generates IL of:

.class public abstract auto ansi sealed beforefieldinit Foo
       extends [mscorlib]System.Object
{
} // end of class Foo

So even a language which doesn't know about static classes will prevent you from deriving another class from it, and prevent you from instantiating it.

Additionally, that's how the C# specification refers to it:

A static class may not include a sealed or abstract modifier. Note, however, that since a static class cannot be instantiated or derived from, it behaves as if it was both sealed and abstract.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
5

CIL doesn't have static classes. CIL has abstract classes, and has sealed classes. An abstract class cannot be instantiated directly (although a concrete class may derive from an abstract class). A sealed class cannot be derived from. Combine those, and you have something that a C# compiler can use to implement static classes.

Incidentally, this fact that CIL does not have static classes means that the restrictions on static classes as generic type arguments don't exist in CIL either. Given a static class X, CIL lets you create what C# would call List<X>. However, given that there cannot be any instances of X or any class derived from it, the only value you can store in such a list is null.

  • For more information, check out the allowed [TypeAttributes](https://msdn.microsoft.com/en-us/library/system.reflection.typeattributes%28v=vs.110%29.aspx) that can be associated with Types in metadata. There are Sealed and Abstract, but no Static. – Dan Bryant Feb 18 '15 at 19:59
  • Actually, we can instantiate an abstract class by instantiating one of its derived classes. `Vehicle v = new Car()` creates an instance of an abstract class. – Shaun Luttin Feb 18 '15 at 20:01
  • @ShaunLuttin No, that creates an instance of `Car`, which is a concrete class. –  Feb 18 '15 at 20:02
  • @hvd All I can do is refer to Erip Lippert's answer here: http://stackoverflow.com/questions/5665691/abstract-classes-vs-static-classes-in-c-sharp "No. That is not true. You can create instances of an abstract class. You do so by creating an instance of a more derived class." – Shaun Luttin Feb 18 '15 at 20:03
  • 1
    @ShaunLuttin Huh. Actually, C# seems to use subtly different terminology from CIL. Eric Lippert's saying an instance of `Car` is also an instance of `Vehicle`. C# shares that point of view. CIL does not: "It is an error to attempt to create an instance of an abstract object type". Reworded to avoid that. –  Feb 18 '15 at 20:08
  • 1
    @ShaunLuttin, a litter further in the answer `"What you cannot do is create an instance of an abstract class that is not also an instance of a more derived concrete class."`. – Habib Feb 18 '15 at 20:08
1

It is implicitly abstract because you cannot instantiate the class.

It is implicitly sealed because you cannot derive from it.

dotnetom
  • 24,551
  • 9
  • 51
  • 54
1

It means it is implicitly abstract since the class cannot be instantiated It is implicitly sealed since you cannot extends or inherit from it.

Nagabhushan Baddi
  • 1,164
  • 10
  • 18