9

What is the difference between these unary operators in C#?

What would an example be? What is the name of each?

+= vs. =+

++x vs. x++

2
  • 3
    @remyabel: I'm pretty sure these operators will behave the same regardless of language.
    – nxasdf
    Commented Feb 6, 2015 at 4:10
  • =+ is not an operator in C#. Take a look at x =+ 2. In this case what you are doing is assigning x to the positive value two. It's the same as x = (+2). To be clear the + does nothing in this context because we are always assuming integers to be positive if not stated otherwise. Here is another example that will probably make more sense: x =- 2 is actually the same as x = (-2) where you are assigning x to the negative value two.
    – Jogge
    Commented Oct 11, 2022 at 8:26

3 Answers 3

30

They differ in how they change the value and how they return the result.

The first two += and =+ behave in the way that the first increments a variable, the other sets a variable. They are not related. Observe the following code:

// +=
x = 1;
printf( x += 1 ); // outputs 2, the same as x = x+1
printf( x );      // outputs 2

// =+
x = 1;
printf( x =+ 1 ); // outputs 1, the same as x = 1;
printf( x );      // outputs 1

The next two, ++x and x++, differ in the order their function. ++x will increment your variable by 1 and return the result. x++ will return the result and increment by 1.

// ++x
x = 1;
printf( ++x ); // outputs 2, the same as x = x+1
printf( x );   // outputs 2

// x++
x = 1;
printf( x++ ); // outputs 1
printf( x );   // outputs 2

They are mostly useful for for loops and while loops.

In terms of speed, ++x is considered a lot faster than x++ since x++ needs to create an internal temporary variable to store the value, increment the main variable, but return the temporary variable, basically more operations are used. I learned this a looong time ago, I don't know if it still applies

1
  • 3
    =+ is not an operator in C#. Take a look at x =+ 2. In this case what you are doing is assigning x to the positive value two. It's the same as x = (+2). To be clear the + does nothing in this context because we are always assuming integers to be positive if not stated otherwise. Here is another example that will probably make more sense: x =- 2 is actually the same as x = (-2) where you are assigning x to the negative value two.
    – Jogge
    Commented Oct 11, 2022 at 8:25
1

Let's visualize the first ones, += and =+.

Because "+" is action, "=" is assignment, so

+= is to add BEFORE assignment

=+ is a bit confusing with "+", it could be "-", for example a=+7 or a=-7, anyway, it's a direct assignment.

Similarly,

++x is "increment then return"

x++ is "return then increase"

3
  • This is actually wrong. =+ simply assigns (the same as =) it turns out.
    – tobbenb3
    Commented Sep 4, 2018 at 13:57
  • This was helpful to understand and remember this. Thanks.
    – Emi
    Commented Jan 7, 2022 at 9:15
  • @tobbenb3 This is actually because =+ is not an operation. Take a look at x =+ 2. In this case what you are doing is assigning x to the positive value two. It's the same as x = (+2). To be clear the + does nothing in this context because we are always assuming integers to be positive if not stated otherwise. Here is another example that will probably make more sense: x =- 2 is actually the same as x = (-2) where you are assigning x to the negative value two.
    – Jogge
    Commented Oct 11, 2022 at 8:18
1

++x vs x++ are unary operators. ++x means pre increment and x++ means post increment.

int temp;
temp = 1;
Console.WriteLine(++temp); // Outputs 2
temp = 1;
Console.WriteLine(temp++); // outputs 1
Console.WriteLine(temp); // outputs 2

Prefix increment means:

The result of the operation is the value of the operand after it has been incremented.

Postfix increment means:

The result of the operation is the value of the operand before it has been incremented.

Now the following: += means temp += 10; // same as temp = temp + 10;

This =+ isn't a valid operator. If one does this:

str = + str;  // will throw an error.
int a;
a = +2; // sort of meaningless . 2 and +2 means same.

More here: Is there such thing as a "=+" operator?

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/increment-operator

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.