Description of the role of sealed in C #

Source: Internet
Author: User
Tags abstract copy modifier
The role of the SEALED keyword:
Using sealed in class declarations prevents other classes from inheriting this class, and using the sealed modifier in the method declaration prevents the extended class from overriding this method.
The sealed modifier is primarily used to prevent unintentional derivation, but it can also cause some run-time 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.
Sealed class:
A sealed class uses the sealed modifier in a declaration, which prevents the class from being inherited by other classes. If you try to use a sealed class as the base class for other classes, C # prompts an error. Of course, sealed classes cannot be abstract classes at the same time, because abstractions always want to be inherited.
In what situations do you use sealed classes? In fact, it is impossible to have a derived class in a sealed class. If a virtual member function exists in a sealed class instance, the member function can be converted to a non-virtual, and the function modifier virtual no longer takes effect.
Let's look at the following example:
Copy CodeThe code is as follows:
Abstract class AbstractClass
{
public abstract void Method ();
}
Sealed class Sealedclass:abstractclass
{
public override 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 the Sealedclass.
Sealing method:
C # also presents the concept of a sealing method (Sealedmethod) to prevent overloading of the method in the derived class of the class in which the method is located. You can use the sealed modifier on a method, which we call the method a sealed method.
Not every member method of a class can be used as sealing method, so it is necessary to overload the virtual method of the base class as a sealing method 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 example code below:
Copy CodeThe code is as follows:
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.WriteLine ("B.G"); }
}
Class C:b
{
Override public void G ()
{
Console.WriteLine ("C.G");
}
}

Class B overloads all 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 B's derived class C, you can overload method G, but you cannot overload method F

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.