Can't use virtual and override on the same method in C#

后端 未结 6 1495
你的背包
你的背包 2020-12-15 04:53

So apparently you cannot use the virtual modifier with the override modifier.

virtual - a method that can be overridden

<
6条回答
  •  攒了一身酷
    2020-12-15 05:33

    This behavior is right. If you don't want inherited class to override your virtual method, you can mark it "sealed" like this:

    public class Line : DrawingObject
     {
           public sealed override void Draw()
           {
                   Console.WriteLine("I'm a Line.");
           }
      }
    

    After that LittleLine class won't be able to override Draw method.

提交回复
热议问题