Why does the Type attribute of a static class show "abstract" in C#?

Viewed 80

Main Code:

public class ReflectionsDemo
{
    public void demo()
    {
        Type t = typeof(StaticClassDemo);
        Console.WriteLine(t.Attributes);
    
    }
}

StaticClassDemo is just a static class with a couple of static methods.

Output: AutoLayout, AnsiClass, Class, Public, Abstract, Sealed

I understand that a static class can't be marked as an abstract class, but why it is having abstract as an attribute?

1 Answers

"Abstract" sort of means "you cannot create an instance of this type"

"Sealed" means "you cannot inherit from this type"

Together, if you squint a little, they describe a static class.

The "static class" feature was not available from the start, so I assume that rather than introduce a new attribute to describe it, the creators used the combination of these existing attributes.

In fact, the language specification says:

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.

Related