Integer summing blues, short += short problem

后端 未结 5 829
野趣味
野趣味 2020-11-22 10:42

Program in 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 usin         


        
5条回答
  •  旧巷少年郎
    2020-11-22 11:05

    Well, the += operator says you'll be increasing the value of a with a short, while = says you'll overwrite the value, with the result of an operation. The operation a + b yields an int, not knowing that it can do otherwise, and you're trying to assign that int to a short.

提交回复
热议问题