问题描述
可能重复:
字节+字节= INT…为什么呢?
我有这样的方法:
void Method(short parameter)
{
short localVariable = 0;
var result = localVariable - parameter;
}
为什么结果的的Int32
而不是一个的Int16
?
推荐答案
这不只是减,目前根本没有exisits短(或字节/为sbyte)运算。
It's not just subtraction, there simply exisits no short (or byte/sbyte) arithmetic.
short a = 2, b = 3;
short c = a + b;
会给它无法转换INT(A + B)短(C)的错误。
Will give the error that it cannot convert int (a+b) to short (c).
还有一个原因几乎从来没有使用短。
One more reason to almost never use short.
另外:在任何计算,短,为sbyte将永远是'加宽'为int,USHORT和字节为uint。这种行为可以追溯到K&放大器; RC(和probaly比更老)
Additional: in any calculation, short and sbyte will always be 'widened' to int, ushort and byte to uint. This behavior goes back to K&R C (and probaly is even older than that).
(旧)原因是,AFAIK,效率,在与炭处理溢出问题。这最后一个原因不成立这么强的C#了,其中一个char是16位,而不是隐式可转换为int。但它是非常幸运的是,C#数值前pressions保持与C和C ++到非常高的程度的兼容。
The (old) reason for this was, afaik, efficiency and overflow problems when dealing with char. That last reason doesn't hold so strong for C# anymore, where a char is 16 bits and not implicitly convertable to int. But it is very fortunate that C# numerical expressions remain compatible with C and C++ to a very high degree.
这篇关于为什么是一个Int16的参数从Int16的变量一个Int32减法的结果呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!