[英]Why can not the Compiler decide the type
I am just curious to know why do we need to append m or M for decimal type?我只是想知道为什么我们需要 append m 或 M 用于十进制类型?
Documentation says there is no implicit conversion 文档说没有隐式转换
I think the compiler has enough information because we declare Decimal
key word.我认为编译器有足够的信息,因为我们声明了Decimal
关键字。
Can some one please explain why can not the compiler determine the value should be treated as decimal but not double.有人可以解释为什么编译器不能确定该值应该被视为十进制而不是双精度。
If you have a statement like this如果你有这样的声明
decimal x = 5.6;
the compiler first looks only at the right-hand side of the assignment ( 5.6
), determines its type ( double
) and then checks if the result can be assigned to the variable on the left-hand side ( x
).编译器首先只查看赋值的右侧( 5.6
),确定其类型( double
),然后检查结果是否可以分配给左侧的变量( x
)。
Since x
is declared as decimal
but the compiler has determined that the expression is of type double
(and there is no implicit conversion from double
to decimal
), the assignment is invalid.由于x
被声明为decimal
,但编译器已确定表达式是double
类型(并且没有从double
到decimal
的隐式转换),因此赋值无效。
(The only exception to this rule is assigning a lambda expression to a delegate variable. In this case, the compiler indeed uses the information at the left-hand side to determine the type of the right-hand side.) (此规则的唯一例外是将 lambda 表达式分配给委托变量。在这种情况下,编译器确实使用左侧的信息来确定右侧的类型。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.