Answer the question
In order to leave comments, you need to log in
How to use (what is the meaning of) sealed?
Here is the documentation
https://docs.microsoft.com/en-us/dotnet/csharp/lan...
Here is an example from the documentation
class A {}
sealed class B : A {}
sealed class A {}
//class B : A {} //Error
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"); }
}
class classA
{
protected string field_1 = null;
public sealed string Field_1 { get { return field_1; } set {field_1 = value;} }
}
class classB : classA
{
public string Field_1 { get { return field_1; } set { if (value != "1") { field_1 = value; } } }
}
class classA
{
protected string field_1 = null;
public sealed virtual string Field_1 { get { return field_1; } set {field_1 = value;} } //sealed нужно убрать отсюда
}
class classB : classA
{
public override string Field_1 { get { return field_1; } set { if (value != "1") { field_1 = value; } } } //sealed должно быть тут
}
Answer the question
In order to leave comments, you need to log in
Why is override required for sealing?
Sealed translates as "sealed, sealed". What exactly is being sealed?
1) Application to class declaration. The first example shows the context in which the keyword is used. The inheritance functionality is sealed at the first level of inheritance. Your counterexample does not provide new information to the compiler, because at compile time the compiler can decide for itself whether a pointer to a virtual function table should be used when storing class objects. Therefore, there the word sealed is syntactically correct, but lexically useless.
2) Application to the declaration of a class member. Sealing implies having something that can be sealed. Namely, the inheritance function. If you need to seal the functionality of inheritance at some level, you do it. If you are trying to print something on
zerothe base class level, then this is, in C# terminology, an unexpressed thought. You shouldn't use inheritance at all in such a case (don't declare virtual). The keyword final (C++) could express such an idea, but in C # it is not, in it OOP is used in a slightly different context (where, roughly speaking, types can be manged and unmanaged, and the programmer does not have full control over sizeof type objects).
When applied to a method or property, the sealed modifier must always be used with override.
Why can I only apply property sealing to the second class?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question