Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the ++ (or --) operator return? [duplicate]

Tags:

operators

c#

While playing around with the ++ operator, I tried to write the following:

++i++;

I expected this to compile at first, but I got a compiler error:

The operand of an increment or decrement operator must be a variable, property or indexer.

I then tried writing ++(i++) to help the compiler understand what I meant but it also (unsurprisingly) didn't work.

So I am left wondering what does the ++ operator return ? With the compiler error I am getting I was expecting ++i to not return an int representing the value of i incremented, but that is also not the case since I can do i = (++i) + 1 with success...

Anybody have any idea why the ++ operator cannot be chained ?

Also, (++i).GetType() does return System.Int32.

like image 643
Bun Avatar asked Jul 23 '14 18:07

Bun


3 Answers

In C# The ++ operator returns the value before (for i++) or after (for ++i) incrementing.

The error you're seeing is because that operator can ONLY be used on a variable, property, or indexer. The reason is because the intent of the operator is to increment the variable that is being referenced. You can't use it on an expression or other type. What would (9++) do?

You expect (i++)++ to be equivalent to i++; i++. But i++ returns a value and internally increments i. Since you can't apply the ++ operator to a value chaining is not possible.

The difference in C++ is that the ++ prefix operator takes a reference as an input, and returns a reference to the original value, so you can chain the operator since it's just using the pointer instead of a value. The postfix operator returns a value and thus cannot be chained.

like image 72
D Stanley Avatar answered Oct 18 '22 22:10

D Stanley


The result of the ++ or -- operator is the value of the variable, property, or indexer, and not the variable, property, or indexer itself.

You can't double-increment a variable like (i++)++ for the same reason you can't increment a value by itself like 100++.

like image 25
p.s.w.g Avatar answered Oct 18 '22 20:10

p.s.w.g


If I can borrow terminology from C or C++, the ++ operator here returns an "r-value". An r-value doesn't map to a language type, it's a syntactical trick to distinguish what's assignable (modifiable) and what isn't.

Something that you can find on the left side of an assignment operation is called an l-value (left value), and things that you can find on the right side of an assignment are "r-values" (right values). The ++ operator has to operate on an l-value, because it needs to assign to it. However, the returned expression has to be an r-value, because it wouldn't make sense to do i++ = 4 in C#, just like it wouldn't make sense to do foo.Bar() = 4 or 1 = 5.

C# doesn't have lvalues or rvalues per se, but it limits what can be found on the left side of an assignment to variables, properties and indexer operations. This is, not coincidentally, also what you can use the ++ operator on.

like image 4
zneak Avatar answered Oct 18 '22 22:10

zneak