c# - Is a += b operator of char implemented same as a = a + b? -


this question has answer here:

found interesting issue following code runs different result:

char c = 'a';  c += 'a';   //passed c = c + 'a'; //cannot implicitly convert type 'int' 'char'. explicit conversion exists (are missing cast?) 

is there difference between a += b , a=a+b, or compiler's code check missed it?

my point why char += char can pass code check while char = (char+char) considered char = int?

c# specification, section 7.17.2 compound assignment:

an operation of form x op= y processed applying binary operator overload resolution (§7.3.4) if operation written x op y. then,

...

• otherwise, if selected operator predefined operator, if return type of selected operator explicitly convertible type of x, , if y implicitly convertible type of x or operator shift operator, operation evaluated x = (t)(x op y), t type of x, except x evaluated once

and that's situation have here. return type of operation int types of x , y both char, , cast automatically inserted us.

(i believe rule exists because there's able insert explicit cast , it's kind of "expected" work, in cases x , y same type)


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -