What are Virtual Methods?

前端 未结 14 573
傲寒
傲寒 2020-11-29 03:02

Why would you declare a method as \"virtual\".

What is the benefit in using virtual?

14条回答
  •  清酒与你
    2020-11-29 03:50

    The Virtual Modifier is used to mark that a method\property(ect) can be modified in a derived class by using the override modifier.

    Example:

    class A
    {
        public virtual void Foo()
           //DoStuff For A
    }
    
    class B : A
    {
        public override void Foo()
        //DoStuff For B
    
        //now call the base to do the stuff for A and B 
        //if required
        base.Foo()
    }
    

提交回复
热议问题