相关疑难解决方法(0)

继承和泛型类型设置

有人可以向我解释为什么下面的代码会输出它的作用吗?为什么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)

c# generics inheritance

5
推荐指数
2
解决办法
241
查看次数

标签 统计

c# ×1

generics ×1

inheritance ×1