相关疑难解决方法(0)

整数蓝色,短+ +短问题

C#中的程序:

short a, b;
a = 10;
b = 10;
a = a + b; // Error : Cannot implicitly convert type 'int' to 'short'.

// we can also write this code by using Arithmetic Assignment Operator as given below

a += b; // But this is running successfully, why?

Console.Write(a);
Run Code Online (Sandbox Code Playgroud)

c# int types short

68
推荐指数
4
解决办法
1万
查看次数

三元运算符行为不一致

以下表达式是可以的

short d = ("obj" == "obj" ) ? 1 : 2;
Run Code Online (Sandbox Code Playgroud)

但是当您像下面一样使用它时,会发生语法错误

short d = (DateTime.Now == DateTime.Now) ? 1 : 2;
Run Code Online (Sandbox Code Playgroud)

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

任何人都可以解释为什么会这样吗?

在三元运算符中比较字符串到字符串和datetime到datetime之间有区别吗,为什么?

如果你能帮助我,我将不胜感激.

c# int casting short conditional-operator

51
推荐指数
2
解决办法
1535
查看次数

为什么从Int16变量Int32中减去Int16参数的结果?

可能重复:
byte + byte = int ...为什么?

我有这样的方法:

 void Method(short parameter)
 {
     short localVariable = 0;
     var result = localVariable - parameter;
 }
Run Code Online (Sandbox Code Playgroud)

为什么结果Int32而不是Int16

.net c# variables

10
推荐指数
2
解决办法
2157
查看次数

短的一元减去变成一个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# ×4

int ×3

short ×3

.net ×1

casting ×1

conditional-operator ×1

types ×1

variables ×1