Integer summing blues, short += short problem

后端 未结 5 821
野趣味
野趣味 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:23

    This happens because int is the smallest signed type for which + is defined. Anything smaller is first promoted to int. The += operator is defined with respect to +, but with a special-case rule for handling results that don't fit the target.

提交回复
热议问题