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) 以下表达式是可以的
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之间有区别吗,为什么?
如果你能帮助我,我将不胜感激.
我有这样的方法:
void Method(short parameter)
{
short localVariable = 0;
var result = localVariable - parameter;
}
Run Code Online (Sandbox Code Playgroud)
为什么结果Int32而不是Int16?
在下面的:
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 ......?