C # Seal Usage

Source: Internet
Author: User

C # Seal Usage


Sealed's Chinese meaning is sealed, so the name is justified, that is, the class or method modified by it will not be inherited or rewritten.

Sealed the role of the keyword:
Using sealed in a class declaration prevents other classes from inheriting this class, and using the sealed modifier in a method declaration prevents an extension class from overriding this method.
The sealed modifier is primarily used to prevent unintended derivation, but it also enables some runtime optimizations. Specifically, because sealed classes never have any derived classes, calls to virtual function members of instances of sealed classes can be converted to non-virtual calls to handle.

Sealing class:
The sealed class uses the sealed modifier in the declaration, which prevents the class from being inherited by other classes. If you attempt to use a sealed class as the base class for other classes, C # will prompt an error. Of course, sealed classes cannot be abstract classes at the same time, because abstractions always want to be inherited.

On what occasions are sealed classes used? In fact, there can be no derived class in a sealed class. If a virtual member function exists in a sealed class instance, the member function can be converted to non-virtual, and the function modifier virtual no longer takes effect.

Let's look at the following example:

1.Abstract classAbstractClass2. {  3. Public Abstract voidMethod (); 4. }  5. 6.Sealed classSealedclass:abstractclass7. {  8. Public Override voidMethod ()9. {//... } Ten. }


If we try to write the following code

1.  class Otherclass:sealedclass   2 .  {   3.  }  


C # will point out this error and tell you that Sealedclass is a sealed class and cannot attempt to derive any class from Sealedclass.


Sealing method:
C # also presents the concept of a sealing method (Sealedmethod) to prevent overloading of the method in a derived class of the class where the method resides. You can use the sealed modifier for a method, which we call the method a sealing method.

Each member method that is not a class can be used as a sealing method, and as a sealing method the virtual method of the base class must be overloaded to provide a concrete implementation method. Therefore, in the declaration of a method, the sealed modifier is always used in conjunction with the override modifier. take a look at the following example code:

1.usingSystem; 2. 3.classA4. {  5. Public Virtual voidF ()6. {   7. Console.WriteLine ("A.F") ; 8. }  9. Ten. Public Virtual voidG () One. {      A. Console.WriteLine ("A.G") ;  -. }   -. }   the.  -.classb:a -. {   -.Sealed Override  Public voidF () +. {    -. Console.WriteLine ("B.f") ;  +. }   A.  at.Override  Public voidG () -. {   -. Console.WriteLine ("B.G") ; }   -. }   -.  -.classc:b in. {   -.Override  Public voidG () to. {    +. Console.WriteLine ("C.G") ;  -. }   the. }

Class B overloads the two virtual methods in base Class A, where the F method uses the sealed modifier to become a sealed method. The G method is not a sealed method, so in the derived class C of B, method G can be overloaded, but method F cannot be overloaded.

C # Seal Usage

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.