is it possible to mark overridden method as final

前端 未结 4 1532
慢半拍i
慢半拍i 2020-12-14 05:49

In C#, is it possible to mark an overridden virtual method as final so implementers cannot override it? How would I do it?

An example may make it easier to understan

4条回答
  •  情深已故
    2020-12-14 06:24

    Yes, with "sealed":

    class A
    {
       abstract void DoAction();
    }
    class B : A
    {
       sealed override void DoAction()
       {
           // Implements action in a way that it doesn't make
           // sense for children to override, e.g. by setting private state
           // later operations depend on  
       }
    }
    class C: B
    {
       override void DoAction() { } // will not compile
    }
    

提交回复
热议问题