简体   繁体   English

为什么编译器不能决定类型

[英]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类型(并且没有从doubledecimal的隐式转换),因此赋值无效。

(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.

相关问题 编译器如何确定是否可以将类型强制转换为其他类型 - How does compiler decide if a Type can be cast to another type 为什么编译器决定2.3是双精度而不是十进制? - Why the compiler decide 2.3 is double and not decimal? 为什么编译器不能从使用中推断出这种类型的参数 - Why can't the compiler infer this type argument from usage 为什么编译器在这种情况下无法确定操作数的类型? - Why compiler can't determine the type of operands in this case? 嵌套泛型:为什么编译器在这种情况下不能推断出类型参数? - Nested Generics: Why can't the compiler infer the type arguments in this case? 泛型:为什么编译器在这种情况下不能推断出类型参数? - Generics: Why can't the compiler infer the type arguments in this case? 为什么编译器不能为自定义集合推断循环变量类型? - Why compiler can not infer loop variable type for custom collections? 为什么编译器不能推断出这个select调用的类型? - Why can't the compiler infer the type for this select call? 使用运算符重载时,C#编译器如何确定哪种实现适用于混合类型操作数? - With operator overloading, how does the C# compiler decide which implementation applies with mixed-type operands? 为什么编译器有时不能隐式推断出包含泛型类型的返回类型? - Why can the compiler sometimes not implicitly infer the return type that contains the generic type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM