Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are static classes sealed?

Tags:

c#

types

oop

I understand what static classes and sealed classes are, and I regularly use extension methods, I'm just wondering—does anyone know why static classes are sealed in C#?

I've looked at MSDN and the C# language specification, but it never really says why they're sealed.

Why shouldn't we be able to inherit from static classes and override static members, etc.?

Edit:

I appreciate your answers, but you're still talking about what a static class is. I know why I can't override its methods. But I'm asking why did they make it that way?

Are vtables really that expensive? Why design a langauge so static classes are literally static? Is it just for tradition? Is there another advantage I'm not seeing?

(I have the sneaking suspicion I'm fundamentally misunderstanding the point of static classes.)

like image 793
Sam Porch Avatar asked Oct 31 '14 02:10

Sam Porch


People also ask

Why static classes are sealed?

As static class is sealed, so no class can inherit from a static class. We cannot create instance of static class that's the reason we cannot have instance members in static class, as static means shared so one copy of the class is shared to all. Static class also cannot inherit from other classes.

Why do we need sealed class?

We use sealed classes to prevent inheritance. As we cannot inherit from a sealed class, the methods in the sealed class cannot be manipulated from other classes. It helps to prevent security issues.

Can sealed class be static?

1. A Sealed class cannot be inherited from and can static and non-static members or methods.

What is difference between sealed and static class?

Static classes are loaded automatically by the . NET Framework common language runtime (CLR) when the program or namespace containing the class is loaded. A sealed class cannot be used as a base class. Sealed classes are primarily used to prevent derivation.


2 Answers

You can't inherit static classes because you can't override static members. You can't override static members because the entire concept of overriding members is dependent on virtual dispatch on the type of the implicit parameter, and static members have no implicit parameter to dispatch on.

like image 51
Servy Avatar answered Sep 23 '22 02:09

Servy


Sealing a class means that you cannot use it as a superclass. Making a class static makes them useless as base classes, because they cannot have overridable methods. Therefore, deriving from a static class has questionable value: one could argue that you could share protected methods from a static base, but then it is a "one-way street", because derived classes cannot alter functionality of their bases by providing useful overrides.

This boils down the utility of static classes to a namespace-like holder of methods and their associated static data. Letting such classes inherit other static classes would make this purpose less clear while adding very little in terms of functionality or convenience.

like image 37
Sergey Kalinichenko Avatar answered Sep 25 '22 02:09

Sergey Kalinichenko