Saturday, February 11, 2023

What does the keyword "virtual" mean in the method definition?

Ans: The method can be over-ridden.


The virtual keyword is used to modify a method, property, indexer, or event declaration and allow for it to be overridden in a derived class.


            public virtual void Add()
            {
                return a + b;
             }


Point to be noted:
  • By default, methods are non-virtual. You cannot override a non-virtual method.
  • You cannot use the virtual modifier with the static, abstract, private, or override modifiers.
  • When a virtual method is invoked, the run-time type of the object is checked for an overriding member. The overriding member in the most derived class is called, which might be the original member, if no derived class has overridden the member.

No comments:

Post a Comment