BitArray and Equals riddle

I have to following code:

BitArray arr1 = new BitArray(200);
BitArray arr2 = new BitArray(200);

arr1[2] = true;
arr2[2] = true;

if (arr1.And(arr2) == arr2)

It returns “bad” for some unknown reason.
My guess is that Equals implemented a reference comparison and not bitwise comparison.
It seems like a strange behavior as BitArray is classic for bitwise operations on it.

Am I missing something?

update: I’ve refactored Microsoft orginal BitArray and named it BitArray2. You can find it here.


Oren Ellenbogen


2 thoughts on “BitArray and Equals riddle

  1. No unknown reason.
    BitArray is a class that doesn’t override Equals or has overloading on ==
    Therefor, it is using the default for classes, which is reference equality.

Comments are closed.