2

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

Minzkraut
  • 2,149
  • 25
  • 31

2 Answers2

8

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.

Community
  • 1
  • 1
Yogesh
  • 14,498
  • 6
  • 44
  • 69
  • Thanks so must. I see at all. So, you can help me to explain the others question. I answer 3 question. Thanks in advance. Maybe we should use string.compare instead of string.Equal(string) ? – Steven Motion Sep 03 '15 at 10:47
  • You can read the question/answer on enums I provided, to get the answer to your second question. I didn't get your third question though. – Yogesh Sep 03 '15 at 12:17
1

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.

Richard Schneider
  • 34,944
  • 9
  • 57
  • 73