إشعار
يتطلب الوصول إلى هذه الصفحة تخويلاً. يمكنك محاولة تسجيل الدخول أو تغيير الدلائل.
يتطلب الوصول إلى هذه الصفحة تخويلاً. يمكنك محاولة تغيير الدلائل.
When you apply the sealed modifier to a class, it prevents other classes from inheriting from that class. In the following example, class B inherits from class A, but no class can inherit from class B.
class A {}
sealed class B : A {}
You can also use the sealed modifier on a method or property that overrides a virtual method or property in a base class. By using this approach, you enable developers to derive classes from your class while preventing them from overriding specific virtual methods or properties.
The C# language reference documents the most recently released version of the C# language. It also contains initial documentation for features in public previews for the upcoming language release.
The documentation identifies any feature first introduced in the last three versions of the language or in current public previews.
Tip
To find when a feature was first introduced in C#, consult the article on the C# language version history.
In the following example, Z inherits from Y but Z can't override the virtual function F that is declared in X and sealed in Y.
class X
{
protected virtual void F() { Console.WriteLine("X.F"); }
protected virtual void F2() { Console.WriteLine("X.F2"); }
}
class Y : X
{
sealed protected override void F() { Console.WriteLine("Y.F"); }
protected override void F2() { Console.WriteLine("Y.F2"); }
}
class Z : Y
{
// Attempting to override F causes compiler error CS0239.
// protected override void F() { Console.WriteLine("Z.F"); }
// Overriding F2 is allowed.
protected override void F2() { Console.WriteLine("Z.F2"); }
}
When you define new methods or properties in a class, you can prevent deriving classes from overriding them by not declaring them as virtual.
When you override a virtual member declared in a base type, you can prevent deriving types from overriding it by using the sealed keyword as shown in the following example:
public sealed override string ToString() => Value;
It's an error to use the abstract modifier with a sealed class, because an abstract class must be inherited by a class that provides an implementation of the abstract methods or properties.
When you apply the sealed modifier to a method or property, always use it with override.
Because structs are implicitly sealed, you can't inherit from them.
For more information, see Inheritance.
For more examples, see Abstract and Sealed Classes and Class Members.
sealed class SealedClass
{
public int x;
public int y;
}
class SealedTest2
{
static void Main()
{
var sc = new SealedClass();
sc.x = 110;
sc.y = 150;
Console.WriteLine($"x = {sc.x}, y = {sc.y}");
}
}
// Output: x = 110, y = 150
In the previous example, you might try to inherit from the sealed class by using the following statement:
class MyDerivedC: SealedClass {} // Error
The result is an error message:
'MyDerivedC': cannot derive from sealed type 'SealedClass'
To determine whether to seal a class, method, or property, generally consider the following two points:
- The potential benefits that deriving classes might gain through the ability to customize your class.
- The potential that deriving classes could modify your classes in such a way that they no longer work correctly or as expected.
C# language specification
For more information, see the C# Language Specification. The language specification is the definitive source for C# syntax and usage.