What is the use of sealed modifier? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What is the use of sealed modifier?

What is the use of sealed modifier, since it doesn't allow a class to be inherited from. But since we're the developers, we could just not make a class inherit them.

27th Nov 2017, 7:41 AM
Ritam Mukherjee
Ritam Mukherjee - avatar
2 Answers
+ 2
1) On a class that implements security features, so that the original object cannot be "impersonated". 2) More generally, i recently exchanged with a person at Microsoft, who told me they tried to limit the inheritance to the places where it really made full sense, because it becomes expensive performance-wise if left untreated. The sealed keyword tells the CLR that there is no class further down to look for methods, and that speeds things up. In most perfomance-enhancing tools on the market nowadays, you will find a checkbox that will seal all your classes that aren't inherited. Be careful though, because if you want to allow plugins or assembly discovery through MEF, you will run into problems.
27th Nov 2017, 9:20 PM
Jamal Kaksouri
Jamal Kaksouri - avatar
+ 2
You cannot inherited from a sealed class. But you can instantiate objects from a sealed class. This means you can use the class with the functionality there is, You cannot extended the class or override methods of the class changing the behaviour of the class. http://www.c-sharpcorner.com/UploadFile/0c1bb2/sealed-class-in-C-Sharp436/
27th Nov 2017, 9:10 PM
sneeze
sneeze - avatar