What's the difference between += and =+?

Is there a difference between the valid += and the mysterious =+ They both compile, is this correct


Answer this question

What's the difference between += and =+?

  • Tsuru

    You got it backwards, += is the valid expression. =+ is the oddity. I'm just trying to figure out what the other's purpose is.

    n += 1 (n = n + 1)
    n =+ 1 ( )

  • Al Dynarski

     Tom Meschter wrote:
    There is no =+ operator in C#; n =+ 1 will be understood by the compiler as n = +1. The unary + operator exists as a complement to the unary - operator (negation), though as you've noticed it doesn't generally do much. :-)

    -Tom Meschter
    Software Dev, Visual C# IDE


    Ah, duh. I shall slap myself repeatedly. Thanks to both!

  • JoeCoder

    There is no =+ operator in C#; n =+ 1 will be understood by the compiler as n = +1. The unary + operator exists as a complement to the unary - operator (negation), though as you've noticed it doesn't generally do much. :-)

    -Tom Meschter
    Software Dev, Visual C# IDE

  • RajKS

    When the following is parsed:

    n =+ 1;

    The compiler will understand it as:

    n = (+1);

    The compiler sees an assignment expression not an addition operation like you might be thinking.

    Curious though is to know how this is considered a valid expression:

    n = +n;

    Anyone



  • DTSlurve

    As mentioned, the unary plus operator is the complement of the unary minus operator. For unary minus:

    n = 42;
    n = -n; // n is now -42

    For unary plus:

    n = 42;
    n = +n; // n is still 42

    The unary plus operator is a NOP, but included for mathematical completeness, as far as I understand.


  • What's the difference between += and =+?