2012-03-31 142 views
-2

我不小心尝试过这个,编译!所以我想知道什么可能这可能意味着谷歌..没有帮助..> +和> - 是什么意思在C#

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

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

两个密码编译顺便说一句..

+12

想一想。 – leppie 2012-03-31 12:35:47

+0

yea正确地评论了leppie – 2012-03-31 12:36:20

+3

点击'Ctrl + K'' Ctrl + D' – CodesInChaos 2012-03-31 12:36:43

回答

10

它解析为

3 > +4 

3 > -4 

于是进入unary +unary -运营商。

如果你想要一个有趣的方式来探索这个,写

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

,然后探索在调试器中产生的表达式树func。你会在树中看到一元运算符。

+0

哦,谢谢你.. – nawfal 2012-03-31 12:36:53

2

3是否大于4?

比3更大吗?

如果你什么东西是做无疑是有史以来,写一个小的测试应用程序:

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

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

    Console.WriteLine((3 > +4)); 
    Console.WriteLine((3 > -4)); 
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> 4(正4),这将总是导致错误。

如果3> -4(负4)总是成立,则第二个成为真。