Should this say that T is int, string or something else? Or should this program not compile in the first place?

It turned out that the actual result is not what I was expecting at least. I learn something new about this language every day.

Can you predict the behaviour of the code? Can you justify it according to the specification? (The specification is really quite difficult to understand on this point, but in fact it does all make sense.)

As far as B inheriting T.... I was trying to infer the inner workings here, to explain why in the world the example would yield int instead of string. Some sort of weird inheritance of T was the only way I could figure that this would happen.

Obviously there's some assumption I'm making that is wrong. Either I don't understand the scoping rules, or I've got the order in which things happen all mixed up.

Took me a few minutes to grasp this one. The <T> is clearly referring to the containing class. Which means that b.M() shows a string. But the container class for c is b - which is A<Int>.

So yes, that makes sense - just took a moment to get there.

Steve

28 Jul 2007 9:45 AM

Hi,

public class A<M> {

public class X<T> {

public class B : A<int> {

public new void M() {

System.Console.WriteLine(typeof(T).ToString());

System.Console.WriteLine(typeof(M).ToString());

}

public class C : B { }

}

}

...

A<string>.X<double>.B.C c = new A<string>.X<double>.B.C();

c.M();

Prints string and double.

And why do I need the new keyword in M here?

Your postings are way shorter than Chris Brumme's when he was still posting, but I am finding myself equally confused after reading them (I love that). So I guess you can consider yourself much more efficient:-)

Excellent example! Why is it that this DOESN'T reproduce the odd behaviour? The germane difference between the two is that in your example, A<T> does not contain anything called B.

When I post what is really happening on Monday it'll be clear why this does what it does.

You need the "new" because your method M shadows the in-scope type variable M from A<M>. We try to make it either illegal to shadow like this, or make you put the keyword "new" on one of them as a big waving red flag that says "there is a name ambiguity involving this method, so keep that in mind when you are reading this code".