C# 为什么((int?)1.GetType()返回typeof(int)?
可能重复:C# 为什么((int?)1.GetType()返回typeof(int)?,c#,.net,.net-4.0,nullable,C#,.net,.net 4.0,Nullable,可能重复: 我希望((int?)1).GetType()返回typeof(int?),但结果它返回typeof(int): 尽管如此,诸如((int?)1.HasValue之类的代码仍然可以很好地编译和运行 为什么将int强制转换为int?会返回int 请注意,如果我用一些int-变量替换常量(1),这也适用 bool a = ((int?)1).GetType() == typeof(int?); // a is false bool b = ((int?)1).GetType() ==
我希望
((int?)1).GetType()
返回typeof(int?
),但结果它返回typeof(int)
:
尽管如此,诸如((int?)1.HasValue
之类的代码仍然可以很好地编译和运行
为什么将int
强制转换为int?
会返回int
请注意,如果我用一些int
-变量替换常量(1
),这也适用
bool a = ((int?)1).GetType() == typeof(int?); // a is false
bool b = ((int?)1).GetType() == typeof(int); // b is true