日期:2014-05-18  浏览次数:21070 次

编译不可通过?short s1 = 1;short s2 = 2;short s3 = s1 + s2; why??
编译不可通过?
C# code
short s1 = 1;
short s2 = 2;
short s3 = s1 + s2; 
why??

------解决方案--------------------
s1 + s2 结果为int
s3 = (short)(s1+s2);
------解决方案--------------------
引自MSDN:
存在从 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

------解决方案--------------------
探讨
s2 s3都是short,两个short相加结果为什么是int?

------解决方案--------------------
探讨
s2 s3都是short,两个short相加结果为什么是int?

------解决方案--------------------
存在从 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 Type Verification Type Verification Type (in stack state) Managed Pointer to Type
int8, unsigned int8, bool int8 int32 int8&
int16, unsigned int16, char int16 int32 int16&
int32, unsigned int32 int32 int32 int32&
int64, unsigned int64 int64 int64 int64&
native int, native unsigned int native int native int native int&
float32 float32 float64 float32&
float64 float64 float64 float64&
Any value type Same type Same type Same type&
Any object type Same type Same type Same type&
Method pointer Same type Same type Not 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 ve