为什么 var 被解析为 Double 而不是 Long?
在下面的代码中,我希望 var
解析为 Int64
,但它解析为 double
。为什么会这样呢?
string a = "1234";
bool asInt = true;
var b = (asInt) ? Int64.Parse(a) : Double.Parse(a) ;
Console.WriteLine(b.GetType());
In the following code, I would expect var
to get resolved to an Int64
, but it gets resolved to a double
. Why is it so?
string a = "1234";
bool asInt = true;
var b = (asInt) ? Int64.Parse(a) : Double.Parse(a) ;
Console.WriteLine(b.GetType());
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
有从
Int64
到Double
的隐式转换,但反之则不然(由于该方向可能会损失精度)。由于条件的两个“分支”需要解析为相同的类型,因此
b
的类型最终被推断为Double
。There is an implicit conversion from
Int64
toDouble
but not the other way (due to possible loss of precision in that direction).Since both "branches" of the conditional need to resolve to the same type, the type of
b
ends up being inferred as aDouble
.您可以将
long
隐式转换为double
。您无法将
double
隐式转换为long
因此,C# 编译器决定变量类型的唯一可能是
double
。请参阅隐式数字转换表。
You can implicitly cast a
long
to adouble
.You cannot implicitly cast a
double
to along
So the C# compiler decided the only possibility for your variable type was
double
.See Implicit Numeric Conversions Table.
因为编译器需要推断出一个可以同时保存
Int64.Parse(a)
和Double.Parse(a)
值的类型,而不需要显式强制转换。如果推断出long
,则表达式的其他部分将丢失精度。如果需要区分类型,则必须声明两个变量并重写代码:
Because the compiler needs to infer a type that can hold values of both
Int64.Parse(a)
andDouble.Parse(a)
without requiring an explicit cast. Iflong
was inferred, precision would be lost in the other porsion of the expression.If you need to distinguish the type, you have to declare two variables and rewrite your code:
C# 编译器根据三元数的两个返回类型之间的公分母来推断类型。 Int64 可以隐式转换为 Double。反之则不然。
请注意,代码示例中布尔值的状态与推断的类型无关。
The C# compiler is inferring the type from the common denominator between the two return types of your ternary. Int64 can be implicitly converted to Double. The inverse is not so.
Note that the state of the Boolean in your code example has nothing to do with the inferred type.
这是
?:
运算符的工作。它应该将所有结果转换为一种类型。PS 您知道的
+
运算符的类似行为:PPS 要获得您想要的内容,您应该使用
object
:PPPS 另一种选择是:
定义 asInt 使用
This is the work of
?:
operator. It should cast all results to one type.P.S. The similar behavior of
+
operator you know:P.P.S. To have what you want you should use
object
:P.P.P.S. Another option is:
to define asInt use
我很惊讶没有人指出如果您知道该值将是合法的
long
值,您可以通过显式强制转换来更改编译器的行为,并且只需使用long
。这可能有帮助,也可能没有帮助,具体取决于确定
asInt
值的条件,以及您打算对表达式结果执行的操作。下面是一个例子:事实上,在这个例子中,你不需要条件运算符;这也可行:
换句话说,最好的解决方案可能不会使用三元运算符,但问题没有提供足够的上下文来了解。
I'm surprised nobody has pointed out that if you know the value will be a legal
long
value, you can change the compiler's behavior with an explicit cast, and just uselong
.This may or may not be helpful, depending on the condition that determines the value for
asInt
, and depending on what you intend to do with the result of the expression. Here's an example:In fact, in this example, you don't need the conditional operator; this would work too:
In other words, it's possible that the best solution wouldn't use the ternary operator, but the question doesn't give enough context to know.