8

可能重复:
可空类型不是可空类型?
为什么 GetType 返回 System.Int32 而不是 Nullable<Int32>?

我预计((int?) 1).GetType()会返回typeof(int?),但事实证明它会返回typeof(int)

bool a = ((int?)1).GetType() == typeof(int?); // a is false
bool b = ((int?)1).GetType() == typeof(int); // b is true

尽管如此,诸如((int?) 1).HasValue编译和运行之类的代码还是很好的。

为什么强制转换一个int返回int?一个int

1请注意,如果我将常量 ( ) 替换为某个int-variable ,这也适用。

4

0 回答 0