3 ms·
It is a general design principle for .net libraries that you should seal a class unless you deliberately and carefully design it to be extensible. If a class i
by goto11 8y ago
It is a general design principle for .net libraries that you should seal a class unless you deliberately and carefully design it to be extensible.
If a class is unsealed you are basically not able to change it ever again, since someone might have subclassed it and depend on all kinds of internal behavior. So it should only be done very deliberately.
- GordonS 8y ago> It is a general design principle for .net libraries Is it? A more typical approach, IMO, is for internal classes to be marked as `internal`, and for `public` classes to be considered part of the public API.
- goto11 8y agoYou do both. If you don't need to expose a class you keep it internal. If you do need to expose it, you keep it sealed unless you deliberate design it for extension. The overarching principle is to keep the API as tight as possible, because as soon as an API is public, you have committed to it and even subtle changes in observable behavior may break some client.
- radicalbyte 8y agoUsually you define an interface for anything that you want to be overridable and provide some way for consumers to provide their own implementations. Then seal your own implementations. Then you only need to design your interfaces in such a way that they can easily be implemented by composition. This is the approach taken in ASP.Net and in WCF; although the latter does a really bad job at making their interfaces user friendly (for even tiny changes you have to re-implement a lot of functionality - which could have been avoided by better interface design).
- tdbgamer 8y agoThis isn't just a design principle for .NET libraries, it's a SOLID design principle. All classes should be inextensible unless they are explicitly designed to be extended.
- davedx 8y agoThe open/closed principle doesn’t say that. It says classes should be “open for extension” (usually meaning you are able to inherit from them and add functionality) and “closed for modification”, meaning you aren’t able to change internal implementations. It does not say “everything should be closed by default unless explicitly marked otherwise”...
- geezerjay 8y agoComposition is still an option, and arguably the default option. Sealing a class does not prevent anyone from extending the functionalities offered by a class.