Sealed keywords in C #

Source: Internet
Author: User

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 #

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.