The role of sealed keywords in C #

Source: Internet
Author: User
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:

Abstract class AbstractClass {public abstract void Method (),} sealed class Sealedclass:abstractclass {public Overrid e void Method () {//...}}



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 (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:

Using System;  Class A {public virtual void F () {Console.WriteLine ("A.F"),} public virtual void G () {Console.WriteLine ("A.G");} } class B:a {sealed override public void F () {Console.WriteLine ("B.f"),} override public void G () {Console.writel INE ("B.G"); }} class C:b {override public void G () {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 derived class C of B, method G can be overloaded, but method F cannot be overloaded

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.