Sealed keyword in association with override

后端 未结 4 1728
无人共我
无人共我 2021-01-07 19:53

Is it always necessary to follow the sealed keyword with override in the signature of a method like the below code:

public sealed o         


        
4条回答
  •  清酒与你
    2021-01-07 20:34

    Well, it technically is possible .... however, the solution is in my option kinda dirty.

    Imagine having a class A (either in your code base or an external library):

    public class A
    {
        public virtual void M () { /* implementation */ }
    }
    

    You could define an (abstract) class B : A as follows:

    public class B : A
    {
        public sealed override void M() => base.M();
    }
    

    Any class C : B would not be able to override A.M as you have sealed the method (even though you made no semantic changes).

提交回复
热议问题