-2

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
  • 66,506
  • 23
  • 132
  • 141
nawfal
  • 66,413
  • 54
  • 311
  • 354

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
  • 228,647
  • 33
  • 413
  • 517
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
  • 66,506
  • 23
  • 132
  • 141
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
  • 44,717
  • 22
  • 115
  • 201