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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With