-1

I had accidentally tried this, which compiles! So I was wondering what could this possibly mean.. google didnt help..

if (3 >+ 4)
   dothis() //this is never hit btw..

if (3 >- 4)
   dothis() //this is hit.

Both the code compile btw..

Darren
  • 68,902
  • 24
  • 138
  • 144
nawfal
  • 70,104
  • 56
  • 326
  • 368
  • yea rightly commented by leppie – Amritpal Singh Mar 31 '12 at 12:36
  • Oh very stupid of me. thanks guys. Can I close the question? – nawfal Mar 31 '12 at 12:38
  • 1
    Btw. did you know the goes to operator `i --> 0` and the comes from operator `0 <-- i`? http://stackoverflow.com/questions/1642028/what-is-the-name-of-this-operator – CodesInChaos Mar 31 '12 at 12:38
  • @CodeInChaos no what does that mean? – nawfal Mar 31 '12 at 12:41
  • 5
    No reason to downvote this into oblivion. The question is clear and has code that exhibits the problem. While the answer may be obvious, sometimes one has a bit of a brainfart. The `-->` question is a bit cooler, but essentially the same thing, and it got hundreds of upvotes. – CodesInChaos Mar 31 '12 at 12:43
  • 1
    @CodeInChaos: agreed; the question is perfectly legit. Hopefully the next guy who runs into this can simply Google for it. – Martin Törnwall Mar 31 '12 at 12:55

3 Answers3

10

It parses as

3 > +4

and

3 > -4

So into the unary + and unary - operators.

If you want an interesting way to explore this, write

Expression<Func<int, int, bool>> func = (x, y) => x >+ y;

and then explore the resulting expression tree func in the debugger. You'll see the unary operator in the tree.

jason
  • 236,483
  • 35
  • 423
  • 525
2

Is 3 greater than 4?

Is 3 greather than -4?

If you're ever in doubt about what something is doing, write a little test app:

  int i = +3;
  int j = -4;

  Console.WriteLine(i);
  Console.WriteLine(j);

  Console.WriteLine((3 > +4));
  Console.WriteLine((3 > -4));
Darren
  • 68,902
  • 24
  • 138
  • 144
2

Try putting a semicolon after dothis() like

dothis();

Then watch what happens to the + and - operator. They will be shifted away from greater or less than sigh and move nearer to 4.

if (3 > +4)
   dothis() //this is never hit btw.. 
            //will never hit in the entire universe

if (3 > -4)
   dothis() //this is hit
            //will always be a hit

First becomes if 3 > +4 (Positive 4) which will always result in false.

Second becomes if 3 > -4 (Negative 4) which will always result in true.

Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208