有人可以向我解释为什么下面的代码会输出它的作用吗?为什么T是一个字符串在第一个,而不是Int32,为什么在下一个输出中它是相反的情况?
这个谜题来自对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();
}
}
Run Code Online (Sandbox Code Playgroud)