【发布时间】:2021-01-29 10:38:13
【问题描述】:
我以这段代码为例:
class A<T> {
public class B : A<long> {
public void f() {
Console.WriteLine( typeof(T).ToString());
}
public class C : A<long>.B { }
}
}
class Prg5 { static void Main() {
var c = new A<float>.B.C();
c.f();
} }
有输出:
System.Int64
为什么要长时间打印类型?最初传递的浮点类型如何以及在何处被替换?
【问题讨论】:
-
因为
B是A<long>? -
所以在 "new A
.B() 中使用浮点数无关紧要?可以放入任何类型并用 long 替换吗?那不应该返回一些错误吗? -
对我来说也很奇怪。我希望
new A.B.C()也能正常工作,但显然不能。 -
我同意,这并不明显。
-
C是一个A<long>.B,所以T里面的f()是long。未使用new A<float>.B.C()中的float。
标签: c# generics types instance inner-classes