什么是双言下之意的目的是什么? [英] what is the purpose of double implying?
问题描述
例如:
const decimal dollars = 25.50M;
为什么我们要补充一点, M
?
为什么不只是做:
const decimal dollars = 25.50;
,因为它已经说小数
,犯规它意味着 25.50
是一个十进制?
since it already says decimal
, doesnt it imply that 25.50
is a decimal?
推荐答案
没有
25.50
的类型是一个独立的表达双击
,而不是小数
。结果
编译器不会看到你试图将其分配到一个小数
变量并把它解释为十进制。
25.50
is a standalone expression of type double
, not decimal
.
The compiler will not see that you're trying to assign it to a decimal
variable and interpret it as a decimal.
除了lambda表达式,匿名方法和条件运算符,所有的C#表达式有一个完全不依赖于上下文中的固定式
Except for lambda expressions, anonymous methods, and the conditional operator, all C# expressions have a fixed type that does not depend at all on context.
想象一下,你希望它是什么,如果编译器确实会发生什么,你叫 Math.Max(1,2)
。结果
Math.Max
有需要重载 INT
,双击
和小数
。哪一个会叫?
Imagine what would happen if the compiler did what you want it to, and you called Math.Max(1, 2)
.
Math.Max
has overloads that take int
, double
, and decimal
. Which one would it call?
这篇关于什么是双言下之意的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!