小编rab*_*ble的帖子

短的一元减去变成一个int?

在下面的:

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

short id = p.mID ?? -p.dID.Value;
Run Code Online (Sandbox Code Playgroud)

编译器给我错误:

错误21无法将类型'int'隐式转换为'short'.存在显式转换(您是否错过了演员?)

我必须将代码更改为以下代码才能工作:

short id = p.mID ?? (short)-p.dID.Value;
Run Code Online (Sandbox Code Playgroud)

好像编译器正在执行类似(int)0 - p.dID.Value,或Int16.operator - 正在返回Int32s ......?

c# int short

8
推荐指数
1
解决办法
2048
查看次数

标签 统计

c# ×1

int ×1

short ×1