3

Possible Duplicate:
Pre and post increment/decrement operators in C#

Consider the following simple snippet:

int x = 0;
int y = ++x + 1; // forks fine, gives y 2 (no doubt :) )

int x = 0;
int y = ++x++; // Error: The operand of an increment or decrement operator must be a variable, property or indexer.

It's obvious that mixing pre/post increments or decrements in the same statement is not a desirable feature for compilers.

But

  1. Isn't, in fact, ++x++ the same as ++x + 1? Even taking into account all the subtleties Eric Lippert uncovers here. They're all about the behavior not the relations with the operand and moreover the message the CSC outputs.
  2. Isn't, in fact, x is a variable? Like, in ++x + 1 that builds fine, the operand is the same x. And it similarly operates over pre-increment operator ++. So this is still being a useful restriction seems a bit odd with its compiler error. Wouldn't it be more meaningful to output, say, "Pre-Increment and Post-increment operators can't be applied simultaneously?
  3. And do you guys know any hidden compiler-perspective reason for this restriction, rather than the code usability one?
Community
  • 1
  • 1
Arman
  • 5,136
  • 3
  • 34
  • 36
  • @OliCharlesworth, these posts intersect of course (though, honestly I couldn't find it before posting this) but the question you mention about is very general (this goes into more details), is not about the compiler message correctness, and to be truth isn't replied well. At least I don't find it is replied well and explains the issues this question points. But thanks for the info, sure :) – Arman Jan 18 '13 at 17:32

3 Answers3

7

The result of (x++) or (++x) is not actually a variable. It's a value, so:

int y = (++x) + 1;
// evaluates to
int y = (1) + 1;

but

int y = ++x++;
// is equivalent to 
int y = ++(x++);
// evaluates to
int y = ++(0);
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
  • (what a name :) ) your reply is much better than in the post mentioned above. But this explains why does C# compiler not allow to write ++x++, ++++x, ++++x++++ e.g because of the restriction it puts over the ++/-- operators. But I really can't understand why does it outputs the current msg which could be more exact with mentioning about the "value restriction". And I can't understand why doesn't allow ++ over a literal value. My question meant that if I can write x = 5 + 1 why I can't I write 5++ for literals. This is probably for another post as I see this is semantically explained. – Arman Jan 18 '13 at 18:13
3

Consider this:

foo()++;

Where foo() returns an int. C# won't like that, because it's protecting you from doing something that you probably didn't intend: The return value of foo() would be incremented but then discarded.

That's all this rule is about: the ++ operator mutates a value, and so you need to be able to access the resulting value by name. So it must be stored in a variable ("bound" to a name). It cannot be applied to a temporary value that has no name.

The C# compiler has some extra magic where it will let you use ++ on a property:

var result = obj.Prop++;

which it expands out into:

var v = obj.Prop;
var temp = v++;
obj.Prop = v;

var result = temp;
Daniel Earwicker
  • 114,894
  • 38
  • 205
  • 284
  • for the foo()++ I'm agree with the error it outputs. foo() is neither variable, property or indexer. but ++x++ is a variable so the message doesn't fit the case. It should have been pointing to the fact that you're applying the post-increment over an expression returning a value but not a variable... The most recent compiler for ex., is more precise in its errors, the async scope as an example. For a regular C# user ++x++ or (++x)++ is simply implies incrementing the value of x two times by one. – Arman Jan 18 '13 at 17:58
  • Another thing is that why to restrict developers with applying post-increment over a value, a literal but not a result expression like foo(). – Arman Jan 18 '13 at 18:00
  • 1
    @Arman Because in the vast majority of cases it would be a bug. `++x++` wouldn't result in `x` being 2, it would result in `x` being one, because the prefix operator is being applied to the result of `x++`, so it's not incrementing `x` itself.If you want `y` to be the result of `x` plus two where `x` is incremented once then write clearer code to begin with: `y = x++ +1;`. – Servy Jan 18 '13 at 18:08
  • 1
    @Arman "but ++x++ is a variable". No, it's not. – Daniel Earwicker Jan 18 '13 at 18:11
  • OK, guys, it's all about not allowing post/pre increment over a value. Though, really can't understand why allow 5 + 1 and restrict 5++ (for literal expressions) - in both cases you can simply write 6. the usage: foo() + 1 is OK for C# but foo()++ is not. Both would make sense for me as to a programmer. – Arman Jan 18 '13 at 18:30
  • @Amran, the primary function of the pre/post-increment operators is to increment a variable in a specific location, the fact that it returns a value is secondary. If you simply want to do arithmetic, that's what arithmetic operators (+) are for. – p.s.w.g Jan 18 '13 at 18:36
  • @Arman - 5++ would mean that the value of 5 should be 6 from now on! – Daniel Earwicker Jan 18 '13 at 23:39
1
  1. Isn't, in fact, ++x++ the same as ++x + 1?

No they are not, u can either perform a pre-increment of post-increment, not both of them at same time

Read this : Pre and post increment/decrement operators in C#

Community
  • 1
  • 1
Parimal Raj
  • 20,189
  • 9
  • 73
  • 110