understanding nested generic classes in C# with quiz

This is an old puzzle, and it is quite difficult. When I gave it to Anders himself he didn’t get the answer right the first time!

I think the version your coworker gave you is from Cyrus’s blog:

http://blogs.msdn.com/b/cyrusn/archive/2005/08/01/446431.aspx

A slightly simpler version is on my blog.

http://blogs.msdn.com/b/ericlippert/archive/2007/07/27/an-inheritance-puzzle-part-one.aspx

The solution to my version is here:

http://blogs.msdn.com/b/ericlippert/archive/2007/07/30/an-inheritance-puzzle-part-two.aspx

Briefly, the reason for the confusing behaviour is that when you have a name that exists both in an outer class and a base class, the base class “wins”. That is, if you have:

public class B
{
  public class X {}
} 
public class P
{
  public class X
  {
    public class D : B
    {
      public class N : X {}
    }
  }
}

Then P.X.D.N inherits from B.X, not from P.X. The puzzle makes nested generic types in such a way that the same declaration can be named via both the “outer” and “base” search paths, but has different meanings in each because of generic construction.

Anyway, read the explanation on the blog posts, and if its still not clear, ask a more specific question.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)