1. Sealed keyword
When the sealed modifier is applied to a class, this modifier prevents other classes from inheriting from the class. Similar to the final keyword in java.
In the following example, Class B inherits from class A, but no class can inherit from Class B.
2. Sealed modifying methods or properties
The ability to allow classes to inherit from base classes and to prevent them from overriding specific virtual methods or virtual properties.
1) Sealed is a virtual method or virtual property, which is used with override, if not a virtual method or a virtual property will report an error: cannot be sealed because it isn't an override
Public class D { /* consoleapplication1.msfun.sealed.d.m () ' * cannot be Sealed because it isn't an override< c8/>* /publicsealedvoid M () {Console.WriteLine (" d.m () " ); }
2) Prevent subclasses from overriding specific methods or properties
Public classA {protected Virtual voidM () {Console.WriteLine ("a.m. ()"); } protected Virtual voidM1 () {Console.WriteLine ("a.m1 ()"); } } Public classb:a {protected Sealed Override voidM () {Console.WriteLine ("b.m ()"); } protected Override voidM1 () {Console.WriteLine ("b.m1 ()"); } } Public Sealed classc:b {/*consoleapplication1.msfun.sealed.c.m () ': * Cannot override inherited member ' consoleapplication1.msfun.sealed.b. M () ' * because it is sealed*/ //protected override void M () {Console.WriteLine ("C.M ()"); protected Override voidM1 () {Console.WriteLine ("c.m1 ()"); } }
Sealed keywords in C #