Does BitArray list a lot of boxing / unboxing?

System.BitArray only implements a universal IEnumerable that returns an object for the IEnumerator.Current property. Does foreach run on top of BitArray - for example

foreach (bool b in bitArray)
{
    // ...
}

and delete all the values ​​of each bit?

Looking at the bitarray enumerator in the reflector, it looks like it makes a new bitmask with every call to MoveNext (), and not something smarter. Is there a more efficient way to enumerate BitArray or replace BitArray with the same storage characteristics? (List <bool> etc. Uses one byte per bool, not one bit, so uses 8x as much space)

+3
source share
1 answer

, . , . , , / ( , , , ). , , , .

, ... , , "". :

public static IEnumerable<bool> EnumerateBitArray(BitArray bitArray)
{
    for (int i=0; i < bitArray.Length; i++)
    {
        yield return bitArray[i];
    }
}

, - !

+5

Source: https://habr.com/ru/post/1712663/