【发布时间】:2012-08-04 22:02:58
【问题描述】:
我有一个代码:
static short Sum(short a, short b)
{
return a + b;
}
而且它不能编译,saynig 不能将 'int' 转换为 'short'。我今天可能真的很累,但我看不到问题!
【问题讨论】:
标签: c# type-conversion
我有一个代码:
static short Sum(short a, short b)
{
return a + b;
}
而且它不能编译,saynig 不能将 'int' 转换为 'short'。我今天可能真的很累,但我看不到问题!
【问题讨论】:
标签: c# type-conversion
而且它不能编译,saynig 不能将 'int' 转换为 'short'。我今天可能真的很累,但我看不到问题!
这就是语言的定义方式。整数类型的 + 运算符定义为:
static uint op +(uint x, uint y)
static int op +(int x, int y)
static ulong op +(ulong x, ulong y)
static long op +(long x, long y)
根据需要提升操作数。
现在至于 原因 为何如此定义 - 老实说,我不知道。我不赞成“因为它可能溢出”的论点——这表明应该将byte + byte 定义为返回short,而int + int 应该返回long,这两者都不是真的。
我在某处听说这可能与性能有关,但我不想肯定地说。 (也许处理器通常只提供对 32 位和 64 位整数的整数运算?)
不管怎样,这并不真正重要为什么会这样 - 这只是语言的规则。
请注意,复合赋值运算符有一个隐式转换回相关类型,所以你可以这样写:
short x = 10;
short y = 20;
x += y;
【讨论】:
return (short) a + b;。但我会警惕微优化......
当您将两个短裤相加时,它们的总和可能超过一个短裤的允许值,但对于 int 来说是一个 OK 值。这几乎就是 Eric Lippert 在his answer here 中所说的。
旁白:为什么添加两个 ints 返回一个 long 不是这种情况? Eric 也提到了这一点:
在一个整数算术环绕它的世界中,在 int 中进行所有计算更为明智,这种类型可能有足够的范围使典型计算不会溢出。
因此,在添加两个 shorts 时定义的 + 运算符返回一个 int。
这就是您收到编译时错误的原因 - 您正在返回一个 int,而您在其中指定了一个 short 返回类型`。
如果您知道添加将总是导致short,则可以将结果显式转换为short:
static short Sum(short a, short b)
{
return (short)(a + b);
}
【讨论】:
int 值相加得到另一个 int 的论据。
int.MaxValue 和1,您将不会得到大于int.MaxValue 的long 值 - 您将得到int.MinValue。
ints 溢出,但不允许shorts。
Jon Skeet 有explained in detail 为什么你的代码不能工作,但他没有列出你需要做什么才能编译它。您可以使用以下内容:
static short Sum(short a, short b)
{
return (short)(a + b);
}
【讨论】:
short x + short y 超过空头最大值的情况;让它抛出异常。 @user970696 导致溢出异常的最简单方法就是 throw new OverflowException();
checked 或unchecked 块来获得所需的行为,而不是手动检查并抛出异常。
return checked((short)(x + y));。