What does the keyword virtual mean in the method definition?

The method can be over-ridden.

Showing Answers 1 - 10 of 10 Answers

Ans:

over-ridden.

  Was this answer useful?  Yes

Keyword virtual is used to make changes to the methods inside the virtual class while runtime. the virtual class should be overide with the same modifier, return type and the name.

  Was this answer useful?  Yes

smandke

  • Aug 27th, 2010
 

Virtual method in C# is a used to support  polymorphism. This means that in a base class we will define a method foo1() with the keyword "virtual" and the derived classes will inherit this behavior of this method. 


say  
class Base
{
      public virtual int foo1()
      {
          return 0;
      }

      public virtual int foo2()
      {
          return 0;
      }

}


Next - if the derived class wants to override this behavior then the override keywork is use
class Derived : Base
{
       public override int foo2()
       {
              return 1; 
        }
}


public class Tester
{
static void main()
{
      Base b = new Base();
     Derived d = new Derived();

    int  first =  b.foo1();
    int second = d.foo1();

// compare values of the 2 variables first and second. - ensure variable values is 0 -  next

    int  third =  b.foo2();
    int forth = d.foo2();  

// compare values of the 2 variables third and forth. - ensure variable values is 0 and 1 

}

}
Hope this clarifies the usage of the keywork virtual and override

Thanks,
Shaila

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions