逻辑与...之间的区别和逻辑&&用于静态代码检查 [英] Difference between logical & and logical && for static code checking

查看:106
本文介绍了逻辑与...之间的区别和逻辑&&用于静态代码检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么

    int DivByZero_2b(int x)

    {

        if((x!= 1)&(x!= 10)&(x!= 97))

          ;  返回1 /((x - 1)*(x - 10)*(x - 97));

       否则返回0;

    }

    int DivByZero_2b(int x)
    {
        if ((x != 1) & (x != 10) & (x != 97))
            return 1 / ((x - 1) * (x - 10) * (x - 97));
        else return 0;
    }

生成警告

    Warnung     7     CodeContracts:可能除以零   

    Warnung    7    CodeContracts: Possible division by zero   

,而

    int DivByZero_2c(int x)

    {

        if((x!= 1)&&(x!= 10)&&(x!= 97))

        ;    返回1 /((x - 1)*(x - 10)*(x - 97));

       否则返回0;

    } b $ b

    int DivByZero_2c(int x)
    {
        if ((x != 1) && (x != 10) && (x != 97))
            return 1 / ((x - 1) * (x - 10) * (x - 97));
        else return 0;
    }

没有这样的警告?

谢谢

理查德

推荐答案

& C#中的运算符是位操作的按位和运算符,而不是逻辑运算符。您可以在条件中使用它,但我们的静态检查器不支持它。我会对使用&以这种方式在我自己的代码中建议
你也不要使用它。

The & operator in C# is the bitwise-and operator for bit manipulation, not the logical-and operator. You can use that in conditions, but it isn't supported by our static checker. I would frown upon use of & in this way in my own code and suggest you don't use it either.


这篇关于逻辑与...之间的区别和逻辑&&用于静态代码检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆