Prakash Kumar
what is the difference between virtual and abstract in c#.net?
By Prakash Kumar in .NET on Oct 27 2016
  • Kapil Bhati
    Sep, 2018 17

    an abstract method must be call override in derived class other wise it will give compile-time error and in virtual you may or may not override it's depend if it's good enough use it.So, abstract methods have no actual code in them, and subclasses HAVE TO override the method. Virtual methods can have code, which is usually a default implementation of something, and any subclasses CAN override the method using the override modifier and provide a custom implementation.public abstract class E {public abstract void AbstractMethod(int i);public virtual void VirtualMethod(int i){// Default implementation which can be overridden by subclasses.} }public class D : E {public override void AbstractMethod(int i){// You HAVE to override this method}public override void VirtualMethod(int i){// You are allowed to override this method.} }

    • 1
  • Prakash Kumar
    Oct, 2016 27

    Virtual methods have an implementation and provide the derived classes with the option of overriding it. Abstract methods do not provide an implementation and forces the derived classes to override the method.

    • 1


Most Popular Job Functions


MOST LIKED QUESTIONS