Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In what way is a static class implicitly abstract?

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?

like image 303
Shaun Luttin Avatar asked Feb 18 '15 19:02

Shaun Luttin


1 Answers

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.

like image 195
Jon Skeet Avatar answered Oct 05 '22 02:10

Jon Skeet