Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

384
Visualizações
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?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

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)

over 4 years ago · Santiago Trujillo Relatório

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.

over 4 years ago · Santiago Trujillo Relatório

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());
     }
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda