【发布时间】:2021-04-28 11:19:22
【问题描述】:
我在原语上的可为空值出现意外行为。
我的测试代码:
Nullable<long> value = long.Parse("5");
Type type = value.GetType();
// at this Point type is System.Int64 and not Nullable<System.Int64>
是否有任何可能性,value 保持 Nullable<System.Int64> 并且不会转换为常规 long?
【问题讨论】:
-
long.Parse返回long,而不是long?- 总是。所以答案肯定是:不。你为什么还要使用long?? -
好吧,
long.Parse返回long而不是long?- 所以变量 的实际内容是long。话虽这么说:如果您重视是null,那么无论如何您都不能调用.GetType()。 -
井值被强类型为 Nullable
。我希望在声明之后使用 Nullable 而不是很长。 -
查看this 文档以阅读如何识别可空值类型。