c# - Is a += b operator of char implemented same as a = a + b? -
this question has answer here:
- difference between a+=1 , a=a+1 in c# 3 answers
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 writtenx op y
. then,...
• otherwise, if selected operator predefined operator, if return type of selected operator explicitly convertible type of
x
, , ify
implicitly convertible type ofx
or operator shift operator, operation evaluatedx = (t)(x op y)
,t
type ofx
, exceptx
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
Post a Comment