首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

编译不可通过?short s1 = 1;short s2 = 2;short s3 = s1 + s2; why?解决方案

2012-03-19 
编译不可通过?short s1 1short s2 2short s3 s1 + s2 why??编译不可通过?C# codeshort s1 1sh

编译不可通过?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 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.



Table 5: Integer Operations
int32int64native intF&O
int32int32Xnative intXXX
int64Xint64XXXX
native intnative intXnative intXXX
--------------------------------------------------
注意第一张表的Verification Type (in stack state)列,真正用于计算的是这一列的数据
根据第一张表,所有的byte,sbyte,short,ushort都会被转成int类型进行计算
再根据第二张表,int op(+-*/%) int的结果是int
把int的值赋值给short的变量,自然需要强制转换

[解决办法]
编译器决定的,没有办法,我觉得尽量少用short类型
[解决办法]
考试题吧。。。。
貌似在那里见到过


[解决办法]

探讨
s2 s3都是short,两个short相加结果为什么是int?

[解决办法]
s1 + s2 在运算时会转化为int类型在计算,所以结果也是int类型
[解决办法]
探讨
s1 + s2 结果为int
s3 = (short)(s1+s2);

[解决办法]
这个是面试题,我以前就遇到过!呵呵。。。贴到这里了。。。。
[解决办法]
s1+s2的值是int型,而s3是short型,你应该显式转换一下
short s3=(short)(s1+s2);
[解决办法]
探讨
如果两个int相加的话,也有越界的可能啊.....

热点排行