Can someone explain to me why the below code outputs what it does? Why is T a String in the first one, not an Int32, and why is it the opposite case in the next output?
Changing the code slightly:
public class A
{
public class B : A
{
public void M() { System.Console.WriteLine(typeof(T)); }
public class C : A.B { }
}
}
public class P
{
public static void Main()
{
(new A.B.C()).M(); //Outputs System.String
}
}
Note how I changed C's base class from B to A. This changes the output from System.Int32 to System.String.
Without that, A derives not from A, but from A, causing the behaviour you've seen. That's because in general, names defined in base classes are available by unqualified lookup, and the name B is defined in the base class A.