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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top