2

Possible Duplicate:
What are Virtual Methods?

In C#, even if you don't declare the base class method as virtual, compiler always calls the latest derived class method when the method signature matches. Without the virtual keyword we just get on warning message stating the derived method will be called(now that can be removed by using new keyword).

What is the use of declaring the method as virtual when without this keyword also the method in the last derived class is called when the signature matches.

I am not understanding something here. Is "virtual" for code readability purpose? Smith

Community
  • 1
  • 1
  • 1
    [MSDN: Virtual](http://msdn.microsoft.com/en-us/library/9fkccyh4(v=vs.71).aspx) – John Woo Oct 02 '12 at 06:28
  • 1
    You are talking about _shadowing_. This is different from overriding. See on [MSDN](http://msdn.microsoft.com/en-us/library/ms172785.aspx) and [here](http://stackoverflow.com/questions/392721/difference-between-shadowing-and-overriding-in-c) – Oded Oct 02 '12 at 06:31

2 Answers2

6

It is not really about "the latest derived method". It's about what happens when you use polymorphism. When you use an instance of a derived class in the context where the parent class is expected, it will call the method of the parent class if you don't use virtual/override.

Example:

class A
{
    public int GetFirstInt() { return 1; }
    public virtual int GetSecondInt() { return 2; }
}

class B : A
{
    public int GetFirstInt() { return 11; }
    public override int GetSecondInt() { return 12; }
}

A a = new A();
B b = new B();

int x = a.GetFirstInt(); // x == 1;
x = a.GetSecondInt();    // x == 2;
x = b.GetFirstInt();     // x == 11;
x = b.GetSecondInt();    // x == 12;

but with the following two methods

public int GetFirstValue(A theA)
{
   return theA.GetFirstInt();
}

public int GetSecondValue(A theA)
{
   return theA.GetSecondInt();
}

this happens:

x = GetFirstValue(a);   // x == 1;
x = GetSecondValue(a);  // x == 2;
x = GetFirstValue(b);   // x == 1!!
x = GetSecondValue(b);  // x == 12
Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
0

A virtual method can be redefined. In the C# language, the virtual keyword designates a method that can be overridden in derived classes. This enables you to add new, derived types without modifying the rest of your program. The runtime type of objects thus determines what your program does.

You can see a detail example.

public class Base
{
  public int GetValue1()
  {
    return 1;
  }

  public virtual int GetValue2()
  {
    return 2;
  }
}

public class Derived : Base
{
  public int GetValue1()
  {
    return 11;
  }

  public override int GetValue2()
  {
     return 22;
   }
}

Base a = new A();
Base b = new B();

b.GetValue1();   // prints 1
b.GetValue2();   // prints 11 
Asif Mushtaq
  • 13,010
  • 3
  • 33
  • 42
  • I understand how "virtual" method works. My point is without this keyword the behavior is same except one warning message. In other words, I can get the full benefit of "virtual" method implementation without using the keyword. Is using this keyword is better practice? – user1492518 Oct 02 '12 at 06:46
  • No, things work quite differently. `virtual` is not syntactic sugar. Please read my reply. – Thorsten Dittmar Oct 02 '12 at 06:51
  • @user1492518 Have a look at the example. – Asif Mushtaq Oct 02 '12 at 07:03