Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

370
Views
Is a += b operator of char implemented same as a = a + b?

Found an interesting issue that following code runs with a different result:

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?)

Is there any difference between a += b and a=a+b, or just compiler's code check missed it?

My point is why char += char can pass the code check while char = (char+char) considered char = int?

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

C# Specification, Section 7.17.2 Compound assignment:

An operation of the form x op= y is processed by applying binary operator overload resolution (§7.3.4) as if the operation was written x op y. Then,

...

• Otherwise, if the selected operator is a predefined operator, if the return type of the selected operator is explicitly convertible to the type of x, and if y is implicitly convertible to the type of x or the operator is a shift operator, then the operation is evaluated as x = (T)(x op y), where T is the type of x, except that x is evaluated only once

And that's exactly the situation we have here. The return type of the operation is int but the types of x and y are both char, and so an extra cast is automatically inserted for us.

(I believe this rule exists because there's nowhere for you to be able to insert an explicit cast yourself and it's kind of "expected" to work, especially in cases where x and y are the same type)

about 4 years ago · Santiago Trujillo Report

0

In the second case it is the assignment that is failing, not the calculation itself. If you explicitly cast the result in the second line to a char it works fine and indeed the following three lines generate identical IL:

    c += (char)1;
    c = (char)(c + (char)1);
    c = (char)(c + 1);

So yes. There is a difference.

Note in the third line I didn't bother casting the 1 to char since the calculation will just convert it back to an int anyway.

about 4 years ago · Santiago Trujillo Report

0

The resulting type of += is char in this case and the resulting type of c + (char)1 is int.

The following code prints:

o1 System.Char o2 System.Int32

    public static void Main(string[] args)
    {
        char c = 'a';

        object o1 = c += (char)1;
        object o2 = c + (char)1;

        WriteLine("o1 " + o1.GetType());
        WriteLine("o2 " + o2.GetType());
     }
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!