Reputation: 21
I read some script and seem to be complicated to understand. Hope someone can explain why The first:
public static bool ContainsDestroyWholeRowColumn(BonusType bt)
{
return (bt & BonusType.DestroyWholeRowColumn)
== BonusType.DestroyWholeRowColumn;
}
Why don't write bt.Equal(BonusType.DestroyWholeRowColumn)
or bt == BonusType.DestroyWhoeRowColumn
?
The Second:
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 input method is not the right Type. I think It will be alert immediately, why they also need to check the type of object The last:
//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)];
I thought these code always return 0; What happened? The writer was wrong or I missed some basic knowledge. Please help me if you know. Thanks
Upvotes: 2
Views: 133
Reputation: 35477
1st Question
a == b
is testing that both a
and b
are the same value.
(a & b) == b
, a
is bitmask (contains multiple bit value) and is checking if the bit b
is on.
Upvotes: 1
Reputation: 14608
This means BonusType
is a flag type enum where multiple values can be combined using bitwise operations.
(bt & BonusType.DestroyWholeRowColumn) == BonusType.DestroyWholeRowColumn
means we are checking whether DestroyWholeRowColumn
flag is set on bt variable.
We can also check the enum flag using Enum.HasFlag method but it is only available .Net 4 onwards.
Check this answer for more information on flag type enums.
Upvotes: 8