为什么是一个Int16的参数从Int16的变量一个Int32减法的结果呢? [英] Why is the result of a subtraction of an Int16 parameter from an Int16 variable an Int32?

查看:320
本文介绍了为什么是一个Int16的参数从Int16的变量一个Int32减法的结果呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  字节+字节= INT…为什么呢?

Possible Duplicate:
byte + byte = int… why?

我有这样的方法:

 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减法的结果呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆