[解决办法] 存在从 short 到 int、long、float、double 或 decimal 的预定义隐式转换。
不能将存储大小更大的非文本数值类型隐式转换为 short(有关整型的存储大小的信息,请参见 整型表(C# 参考))。例如,请看以下两个 short 变量 x 和 y:
short x = 5, y = 12;
以下赋值语句将产生一个编译错误,原因是赋值运算符右侧的算术表达式在默认情况下的计算结果为 int 类型。
short z = x + y; // Error: no conversion from int to short
若要解决此问题,请使用强制转换:
short z = ( short )(x + y); // OK: explicit conversion [解决办法] 这个问题涉及cli的定义, 参考: Common Language Infrastructure (CLI) Partition III CIL Instruction Set Final Draft, Apr 2005 --------------------------------------------- In the following table, “CLI Type” is the type as it is described in metadata. The “Verification Type” is a corresponding type used for type compatibility rules in verification (see §1.8.1.2.2) when considering the types of local variables, arguments, and parameters on methods being called. The column “Verification Type (in stack state)” is used to simulate instructions that load data onto the stack, and shows the types that are actually maintained in the stack state information of the verification algorithm. The column “Managed Pointer to Type” shows the type tracked for managed pointers.
CLI TypeVerification TypeVerification Type (in stack state)Managed Pointer to Type int8, unsigned int8, boolint8int32int8& int16, unsigned int16, charint16int32int16& int32, unsigned int32int32int32int32& int64, unsigned int64int64int64int64& native int, native unsigned intnative intnative intnative int& float32float32float64float32& float64float64float64float64& Any value typeSame typeSame typeSame type& Any object typeSame typeSame typeSame type& Method pointerSame typeSame typeNot valid
These operate only on integer types. Used for and, div.un, not, or, rem.un, xor. The div.un and rem.un instructions treat their operands as unsigned integers and produce the bit pattern corresponding to the unsigned result. As described in the CLI standard, however, the CLI makes no distinction between signed and unsigned integers on the stack. The not instruction is unary and returns the same type as the input. The shl and shr instructions return the same type as their first operand, and their second operand shall be of type int32 or native int. Boxes marked X indicate invalid CIL sequences. All other boxes denote verifiable combinations of operands.