In C#,sealed
Keywords are used to prevent a class from inheriting, or to prevent a member (such as a method, property, indexer, or event) from being overwritten. When a class is declared sealed, it cannot have subclasses, that is, other classes cannot inherit from it.
When a member (such as a method, property, indexer, or event) is declared as sealed, it cannot be overridden in the derived class. This is useful for preventing derived classes from modifying the behavior of specific members.
usesealed
The basic syntax of keywords is as follows:
sealed class MyClass { // ... } sealed override void MyMethod() { // ... }
It should be noted thatsealed
Keywords can only be used withclass
Use together, not withinterface
orabstract
Use the class together. at the same time,sealed
Keywords can only be used withoverride
Keywords are used together and cannot be used alone in methods, properties, indexers, or events.
For example, the following code defines a sealed class and a sealed method:
public class MyBaseClass { public virtual void MyMethod() { ("Base class method."); } } public sealed class MySealedClass : MyBaseClass { public sealed override void MyMethod() { ("Sealed class method."); } }
In this example,MySealedClass
Can't be inherited,MyMethod
Cannot be rewritten in any derived class.
This is the article about the specific use of sealed keywords in C#. For more related C# sealed keyword content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!