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) 发现一个有趣的问题,以下代码运行时具有不同的结果:
char c = 'a';
c += 'a'; //passed
c = c + 'a'; //Cannot implicitly convert type 'int' to 'char'. An explicit conversion exists (are you missing a cast?)
Run Code Online (Sandbox Code Playgroud)
有什么区别a += b
和a=a+b
,或只是编译器的代码检查错过呢?
我的观点是为什么char += char
在char = (char+char)
考虑时可以通过代码检查char = int
?