Sealed modifier learning in C #

Source: Internet
Author: User
Tags modifiers

Reprint Original Address http://developer.51cto.com/art/200908/147327.htm

C # language is more common, here we mainly introduce C # sealed modifier, including the introduction of two modifiers in the meaning of mutually exclusive use of methods and properties.

What does the C # sealed modifier do?

The C # sealed modifier means that when sealed for a class, it means that the class can no longer be inherited and cannot be used in conjunction with abstract because the two modifiers are mutually exclusive to methods and properties, meaning that the method or property can no longer be overridden and must be used with the override keyword because the The method or property of the C # sealed modifier is certainly the corresponding virtual member in the base class that is typically used to implement a third-party class library when it does not want to be inherited by the client, or for classes that do not need to be inherited to prevent misuse of inheritance resulting in a hierarchical system confusion proper use of the C # sealed modifier can also improve some operational efficiency This member is overridden because the inheriting class is not considered.

Example:

usingSystem; 2.usingSystem.Collections.Generic; 3.usingSystem.Text; 4. 5.namespaceExample066. {  7.class Program8. {  9.classATen. {   One. Public Virtual voidF () A. {   -. Console.WriteLine ("A.F");  -.}  the. Public Virtual voidG () -. {   -. Console.WriteLine ("A.G");  -.}  +.}  -.classb:a +. {   A. Public Sealed Override voidF () at. {   -. Console.WriteLine ("B.f");  -.}  -. Public Override voidG () -. {   -. Console.WriteLine ("B.G");  in.}  -.}  to.classc:b +. {   -. Public Override voidG () the. {   *. Console.WriteLine ("C.G");  $.} Panax Notoginseng.}  -.Static voidMain (string[] args) the. {   +.NewA ().  F ();  A.NewA ().  G ();  the.NewB ().  F ();  +.NewB ().  G ();  -.NewC ().  F ();  $.NewC ().  G ();  $.  -.  Console.ReadLine ();  -.}  the.}  -.}

Reprint Original Address http://www.cnblogs.com/iamdaiyuan/archive/2007/02/06/642442.html

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 Sealed classSealedclass:abstractclass6 {7         Public Override voidMethod ()8{//... }9}

If we try to write the following code

class otherclass:sealedclass{}

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 (sealed methods) 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 classA3 { 4      Public Virtual voidF ()5{Console.WriteLine ("A.F") ; } 6              Public Virtual voidG ()7{Console.WriteLine ("A.G") ; } 8 } 9 classb:aTen { One     Sealed Override  Public voidF () A{Console.WriteLine ("B.f") ; } -         Override  Public voidG () -{Console.WriteLine ("B.G") ; } the     } -     classc:b - { -     Override  Public voidG () +{Console.WriteLine ("C.G") ; } -}

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.

Sealed modifier learning in C #

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.