c# - 为什么这段代码用long替换float?
问题描述
我以这段代码为例:
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
为什么它会长时间打印类型?最初传递的浮点类型是如何以及在哪里被替换的?
解决方案
类型C
定义为:
public class C : A<long>.B { }
类型A
定义为:
class A<T> {
public class B : A<long> {
public void f() {
Console.WriteLine( typeof(T).ToString());
}
public class C : A<long>.B { }
}
}
因此,如果您创建一个新C
的,则类型参数A
为long
。
在语句中var c = new A<float>.B.C();
,A<float>
和B
只是嵌套类的“路径”的一部分C
。作为该路径的一部分的事实A<float>
不会改变.C
A<long>.B
The float
parameter and the fact that B
is an A<long>
are not relevant to determine the type of T
inside c.f()
.
推荐阅读
- python - 用透视图绘制 3-D 曲面和线
- amazon-web-services - 在 VPC 中使用 Lambda 和 SNS+RDS
- angular - 将数据从 servlet javaEE 发送到 Angular 组件
- python - 如何从 django 中的令牌获取 userId
- cookies - Cypress.io:为什么 cy.route 没有设置 cookie?
- c - 为什么用指针声明一个字符串数组?这样做时会发生什么?
- php - 并排显示一条 Union 语句
- c# - 从 TextBox 到 DataGridView 的动态值
- javascript - text/plain 和 string 之间有区别吗?
- javascript - React js 组件渲染