sealed means sealed in Chinese, so the name suggests that the class or method modified by it cannot be inherited or overridden.
The role of the sealed keyword:
Using sealed in a class declaration can prevent other classes from inheriting this class; using the sealed modifier in a method declaration can prevent extended classes from overriding this class. method.
The sealed modifier is mainly used to prevent unintentional derivation, but it can also promote certain runtime optimizations. Specifically, since a sealed class will never have any derived classes, calls to virtual function members of instances of the sealed class can be converted to non-virtual calls for processing.
Sealed class:
Sealed classes use the sealed modifier in the declaration to prevent the class from being inherited by other classes. If you try to use a sealed class as a base class for other classes, C# will prompt an error. Of course, a sealed class cannot be an abstract class at the same time, because abstractions always want to be inherited.
In what situations are sealed classes used? In fact, it is impossible to have a derived class within 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 will no longer take effect.
Let’s look at the following example:
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 you cannot try to derive any class from SealedClass.
Sealed method:
C# also proposes the concept of sealed method (sealedmethod) to prevent the method from being overloaded in a derived class of the class where the method is located. You can use the sealed modifier on a method, then we call the method a sealed method.
Not every member method of a class can be used as a sealed method. To be used as a sealed method, the virtual method of the base class must be overloaded and a specific implementation method must be provided. Therefore, in a method declaration, the sealed modifier is always used together with the override modifier. Please 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.WriteLine("B.G") ; } } class C: B { override public void G( ) { Console.WriteLine("C.G") ; } }
Class B overloads both virtual methods in base class A. Method F uses the sealed modifier and becomes a sealed method. The G method is not a sealed method, so in the derived class C of B, the method G can be overloaded, but the method F
The above is the detailed content of Detailed explanation of the role of sealed keyword in C#. For more information, please follow other related articles on the PHP Chinese website!