Delegate invoked on a virtual method? What happens

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

If I hook a delegate directly to a virtual method... does it call the base
class or the derived?
 
It calls the derived class method, tested using this code

class Class1
{
[STAThread]
static void Main(string[] args)
{
Base b = new Derived();
MethodInvoker mi = new MethodInvoker(b.Test);
mi();
System.Console.ReadLine();
}
}

public class Base
{
public virtual void Test()
{
System.Console.WriteLine("Base");
}
}

public class Derived : Base
{
public override void Test()
{
System.Console.WriteLine("Derived");
}
}

Output is "Derived".

Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph
 
Back
Top