RE: short s = 1; s += 1; 不会出错. --------------- 这样是不会错 但 s= s+ 1就错了 我想问这样要怎么转换那个 1
short短整形 System.Int16 不能乱赋值的要用Convert转换一下
或者: short s = 1; s = Convert.ToInt16(s + 1);又或者 s = (short)(s + 1);
short s = 1; s= (short)(s+ Convert.ToByte(1)); Console.WriteLine(s); 这样就行了.
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
s += 1;short占两个字节
byte一个字节怎么会一样啊
还是出错
所以你加编译会出错的.
s += 1;不会出错.
short s = 1;
s += 1; 不会出错.
---------------
这样是不会错
但
s= s+ 1就错了
我想问这样要怎么转换那个 1
s = Convert.ToInt16(s + 1);又或者 s = (short)(s + 1);
s= (short)(s+ Convert.ToByte(1));
Console.WriteLine(s);
这样就行了.
以下赋值语句将产生一个编译错误,原因是赋值运算符右侧的算术表达式在默认情况下的计算结果为 int 类型。short z = x + y; // Error: no conversion from int to short 若要解决此问题,请使用强制转换:short z = ( short )(x + y); // OK: explicit conversion
short s = 1;
s += 1;