> +和> - 在C#中的含义是什么

时间:2012-03-31 12:33:49

标签: c# .net operators

我不小心试过这个,编译!所以我想知道这可能是什么意思..谷歌没有帮助..

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

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

两个代码都编译btw ..

3 个答案:

答案 0 :(得分:10)

它解析为

3 > +4

3 > -4

进入unary +unary -运营商。

如果您想要一种有趣的方式来探索它,请写下

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

然后在调试器中浏览生成的表达式树func。你会在树中看到一元运算符。

答案 1 :(得分:2)

3大于4吗?

3大于-4?

如果你对某些事情做了什么有疑问,请写一个小测试应用程序:

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

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

  Console.WriteLine((3 > +4));
  Console.WriteLine((3 > -4));

答案 2 :(得分:2)

尝试在dothis()之后添加分号,如

dothis();

然后观察+和 - 运算符会发生什么。它们会从大于或小于叹息的位置移开,然后移近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

如果3&gt; +4(正4)总是会导致错误。

如果3&gt;则成为第二个-4(负4)总是会产生真实的。