相关疑难解决方法(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万
查看次数

短+短!=短?

版本:Visual Studio Professional 2013 Update 4
构建参数:首选32位为真

我不明白以下C#代码中的错误:

short iCount = 20;
short iValue = iCount + (short)1;
Run Code Online (Sandbox Code Playgroud)

将short添加到转换为short的int会导致以下错误:

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

上面的错误,在下面的例子中也可以看到,在这里完全有效:

short iCount = 20;
short iValue = iCount + 1;
Run Code Online (Sandbox Code Playgroud)

以下解决方法将删除错误:

short iCount = 20;
short iValue = (short)(iCount + 1);
Run Code Online (Sandbox Code Playgroud)

因此,"short + Int32 constant"形式的加法显然有效,结果是Int32,需要将其缩短.

有没有解释为什么第一个代码示例失败或者这是编译器错误?(在4次更新后我几乎无法相信)

c# short visual-studio-2013

6
推荐指数
1
解决办法
453
查看次数

标签 统计

c# ×2

short ×2

int ×1

types ×1

visual-studio-2013 ×1