有人可以向我解释为什么下面的代码输出它的作用? 为什么T是第一个字符串,而不是Int32,为什么它在下一个输出中是相反的情况?继承和泛型类型设置
这让人不解的是从interview with Eric Lippert
当我看到通过代码,我真的不知道,如果它的将是一个Int32或String:
public class A<T>
{
public class B : A<int>
{
public void M() { System.Console.WriteLine(typeof(T)); }
public class C : B { }
}
}
public class P
{
public static void Main()
{
(new A<string>.B()).M(); //Outputs System.String
(new A<string>.B.C()).M(); //Outputs System.Int32
Console.Read();
}
}
https://blogs.msdn.microsoft.com/ericlippert/2007/07/30/an-inheritance-puzzle-part-two/ –