在短短的一元减变成一个int? [英] Unary minus on a short becomes an int?

查看:105
本文介绍了在短短的一元减变成一个int?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下几点:

public class p
{
  short? mID;
  short? dID;
}

short id = p.mID ?? -p.dID.Value;



编译器给我的错误:

The compiler gives me the error:

错误21无法隐式转换类型'诠释'到'短'。一个显式转换存在(是否缺少强制转换?)

Error 21 Cannot implicitly convert type 'int' to 'short'. An explicit conversion exists (are you missing a cast?)

我要改变代码以下为它工作:

I have to change the code to the following for it to work:

short id = p.mID ?? (short)-p.dID.Value;



这是因为如果编译器做这样的事情(INT)0 - p.dID.Value,或这Int16.operator - 正在恢复Int32s ...

It's as if the compiler is doing something like (int)0 - p.dID.Value, or that Int16.operator - is returning Int32s...?

推荐答案

我是指你的规范的第7.6.2,这状态:

I refer you to section 7.6.2 of the specification, which states:

有关-x形式的操作,一元运算符重载分析被用于选择特定的运算符实现。的操作数转换为参数类型所选运营商的,并且结果的类型是运营商的返回类型。预定义的否定运算符是:

For an operation of the form –x, unary operator overload resolution is applied to select a specific operator implementation. The operand is converted to the parameter type of the selected operator, and the type of the result is the return type of the operator. The predefined negation operators are:

整数否定:

int operator –(int x);
long operator –(long x);



结果由从零减去x来计算。如果x的值是操作数类型(int或-2 ^ 63长期-2 ^ 31)的最小可表示值,则x的算术否定没有操作数类型可表示范围内。如果选中的上下文中出现,则引发System.OverflowException;如果一个未经检查的背景下发生的,其结果是操作数的值,不报告溢出。
如果否定运算符的操作数是uint类型,它被转换为long类型,并且结果的类型为长。一个例外是,允许int值规则-2147483648(-2 ^ 31)写为十进制整数文字。

The result is computed by subtracting x from zero. If the value of of x is the smallest representable value of the operand type (−2^31 for int or −2^63 for long), then the mathematical negation of x is not representable within the operand type. If this occurs within a checked context, a System.OverflowException is thrown; if it occurs within an unchecked context, the result is the value of the operand and the overflow is not reported. If the operand of the negation operator is of type uint, it is converted to type long, and the type of the result is long. An exception is the rule that permits the int value −2147483648 (−2^31) to be written as a decimal integer literal .

如果否定运算符的操作数是ulong类型,则发生编译时错误。一个例外是,允许长值-9223372036854775808(-2 ^ 63)写为十进制整数文字

If the operand of the negation operator is of type ulong, a compile-time error occurs. An exception is the rule that permits the long value −9223372036854775808 (−2^63) to be written as a decimal integer literal .

浮点否定规则:

float operator –(float x);
double operator –(double x);



结果是x的,其符号反转的价值。若x是NaN,那么结果也为NaN

The result is the value of x with its sign inverted. If x is NaN, the result is also NaN.

小数否定:

decimal operator –(decimal x);



结果由从零减去x来计算。小数否定等效于使用System.Decimal类型的一元减运算符。

The result is computed by subtracting x from zero. Decimal negation is equivalent to using the unary minus operator of type System.Decimal.

正如你所看到的,有没有短裤定义元减运算符;重载决议在挑选的整数之一,因为,一个是所有可用的一元减去运营商的最佳匹配。

As you can see, there is no unary minus operator defined on shorts; overload resolution picks the one on ints because that one is the best match of all the available unary minus operators.

这篇关于在短短的一元减变成一个int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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