Putting a base in the middle

Posted by PSteele on ASP.net Weblogs See other posts from ASP.net Weblogs or by PSteele
Published on Tue, 06 Apr 2010 13:36:15 GMT Indexed on 2010/04/06 13:43 UTC
Read the original article Hit count: 625

Filed under:

From Eric Lippert's Blog:

Here’s a crazy-seeming but honest-to-goodness real customer scenario that got reported to me recently. There are three DLLs involved, Alpha.DLL, Bravo.DLL and Charlie.DLL. The classes in each are:

public class Alpha // In Alpha.DLL
{
  public virtual void M()
  {
    Console.WriteLine("Alpha");
  }
}

public class Bravo: Alpha // In Bravo.DLL
{
}

public class Charlie : Bravo // In Charlie.DLL
{
  public override void M()
  {
    Console.WriteLine("Charlie");
    base.M();
  }
}

Perfectly sensible. You call M on an instance of Charlie and it says “Charlie / Alpha”.

Now the vendor who supplies Bravo.DLL ships a new version which has this code:

public class Bravo: Alpha
{
  public override void M()
  {
    Console.WriteLine("Bravo");
    base.M();
  }
}

The question is: what happens if you call Charlie.M without recompiling Charlie.DLL, but you are loading the new version of Bravo.DLL?

The customer was quite surprised that the output is still “Charlie / Alpha”, not “Charlie / Bravo / Alpha”.

Read the full post for a very interesting discussion of the design of C#, the CLR, method resolution and more.

Technorati Tags: ,,

© ASP.net Weblogs or respective owner

Related posts about .NET