从 float 和 double 到 C# 5.0 规范(第 6.2.1 段)中描述的任何整数类型的显式数字转换如下:
• 对于从 float 或 double 到整数类型的转换,处理取决于发生转换的溢出检查上下文(第 7.6.12 节):
o In a checked context, the conversion proceeds as follows: • If the value of the operand is NaN or infinite, a System.OverflowException is thrown. • Otherwise, the source operand is rounded towards zero to the nearest integral value. If this integral value is within the range of the destination type then this value is the result of the conversion. • Otherwise, a System.OverflowException is thrown. o In an unchecked context, the conversion always succeeds, and proceeds as follows. • If the value of the operand is NaN or infinite, the result of the conversion is an unspecified value of the destination type. • Otherwise, the source operand is rounded towards zero to the nearest integral value. If this integral value is within the range of the destination type then this value is the result of the conversion. • Otherwise, the result of the conversion is an unspecified value of the destination type.
同时在MSDN中描述的相同转换规则如下:
当您从 double 或 float 值转换为整数类型时,该值将被截断。如果生成的整数值超出目标值的范围,则结果取决于溢出检查上下文。在已检查的上下文中,会引发 OverflowException,而在未检查的上下文中,结果是目标类型的未指定值。
评估这种转换,例如“(int)123.566”,得到“123”。规范中的描述是否正确?