Why seal a class?
.net, performance
Solution
- Sometimes classes are too precious and not designed to be inherited.
- Runtime/Reflection can make inheritance assumptions about sealed classes when looking for types. A great example of this is - Attributes are recommended to be sealed for lookup runtime speed. type.GetCustomAttributes(typeof(MyAttribute)) will perform significantly faster if MyAttribute is sealed.
The MSDN article for this topic is Limiting Extensibility by Sealing Classes.
Problem
I'd like to hear what is the motivation behind the bulk of sealed classes in the .Net framework. What is the benefit of sealing a class? I cannot fathom how not allowing inheritance can be useful and most likely not the only one fighting these classes. So, why is the framework designed this way and wouldn't it be unbreaking change to unseal everything? There must be another reason than just being evil?