Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# a==b和(a&b)==b之间的区别是什么_C#_Boolean Operations - Fatal编程技术网

C# a==b和(a&b)==b之间的区别是什么

C# a==b和(a&b)==b之间的区别是什么,c#,boolean-operations,C#,Boolean Operations,我读了一些剧本,似乎很难理解。希望有人能解释一下原因 第一: public static bool ContainsDestroyWholeRowColumn(BonusType bt) { return (bt & BonusType.DestroyWholeRowColumn) == BonusType.DestroyWholeRowColumn; } 为什么不写bt.Equal(BonuType.DestroyWhorRo

我读了一些剧本,似乎很难理解。希望有人能解释一下原因 第一:

public static bool ContainsDestroyWholeRowColumn(BonusType bt)
    {
        return (bt & BonusType.DestroyWholeRowColumn) 
            == BonusType.DestroyWholeRowColumn;
    }
为什么不写
bt.Equal(BonuType.DestroyWhorRowColumn)
bt==BonuType.DestroyWhorRowColumn
? 第二点:

public bool IsSameType(Shape otherShape)
    {
        if (otherShape == null || !(otherShape is Shape))// check otherShape is not null and it is Shape
            throw new ArgumentException("otherShape");

        return string.Compare(this.Type, (otherShape as Shape).Type) == 0;
    }
如果输入法不是正确的类型。我想它会立即发出警报,为什么他们还需要检查对象的类型 最后:

//if we are in the middle of the calculations/loops
            //and we have less than 3 matches, return a random one
            if(row >= Constants.Rows / 2 && matches.Count > 0 && matches.Count <=2)
                return matches[UnityEngine.Random.Range(0, matches.Count - 1)];
<代码> / /如果我们处于计算/循环的中间 //我们只有不到3个匹配项,返回一个随机匹配项
如果(行>=Constants.Rows/2&&matches.Count>0&&matches.Count这意味着
BonusType
是一个标志类型枚举,其中可以使用位操作组合多个值

(bt&BonuType.DestroyWholeRowColumn)=BonuType.DestroyWholeRowColumn
表示我们正在检查bt变量上是否设置了
DestroyWholeRowColumn
标志

我们也可以使用方法检查枚举标志,但它仅适用于.Net 4以后的版本


查看有关标志类型枚举的更多信息。

第一个问题

a==b
正在测试
a
b
的值是否相同。
(a&b)==b
a
是位掩码(包含多个位值),并且正在检查位
b
是否打开。

3&1==1
,但是
3!=1
@Mat:虽然这里不是整数类型(我想,问题没有说)是的。a和b不是整数,在这种情况下,它是BonuTypeThank,所以必须。我明白了。所以,你可以帮我解释其他问题。我回答了3个问题。提前谢谢。也许我们应该使用string.compare而不是string.Equal(string)?您可以阅读我提供的Enum上的问题/答案,以获得第二个问题的答案。不过,我没有收到您的第三个问题。