BitArray Class
Manages a compact array of bit values, which are represented as Booleans, where true indicates that the bit is on (1) and false indicates the bit is off (0).
Assembly: mscorlib (in mscorlib.dll)
The size of a BitArray is controlled by the client; indexing past the end of the BitArray throws an ArgumentException.
Elements in this collection can be accessed using an integer index. Indexes in this collection are zero-based.
The following code example shows how to create and initialize a BitArray and how to print out its values.
Imports System Imports System.Collections Public Class SamplesBitArray Public Shared Sub Main() ' Creates and initializes several BitArrays. Dim myBA1 As New BitArray(5) Dim myBA2 As New BitArray(5, False) Dim myBytes() As Byte = {1, 2, 3, 4, 5} Dim myBA3 As New BitArray(myBytes) Dim myBools() As Boolean = {True, False, True, True, False} Dim myBA4 As New BitArray(myBools) Dim myInts() As Integer = {6, 7, 8, 9, 10} Dim myBA5 As New BitArray(myInts) ' Displays the properties and values of the BitArrays. Console.WriteLine("myBA1") Console.WriteLine(" Count: {0}", myBA1.Count) Console.WriteLine(" Length: {0}", myBA1.Length) Console.WriteLine(" Values:") PrintValues(myBA1, 8) Console.WriteLine("myBA2") Console.WriteLine(" Count: {0}", myBA2.Count) Console.WriteLine(" Length: {0}", myBA2.Length) Console.WriteLine(" Values:") PrintValues(myBA2, 8) Console.WriteLine("myBA3") Console.WriteLine(" Count: {0}", myBA3.Count) Console.WriteLine(" Length: {0}", myBA3.Length) Console.WriteLine(" Values:") PrintValues(myBA3, 8) Console.WriteLine("myBA4") Console.WriteLine(" Count: {0}", myBA4.Count) Console.WriteLine(" Length: {0}", myBA4.Length) Console.WriteLine(" Values:") PrintValues(myBA4, 8) Console.WriteLine("myBA5") Console.WriteLine(" Count: {0}", myBA5.Count) Console.WriteLine(" Length: {0}", myBA5.Length) Console.WriteLine(" Values:") PrintValues(myBA5, 8) End Sub 'Main Public Shared Sub PrintValues(myList As IEnumerable, myWidth As Integer) Dim i As Integer = myWidth Dim obj As [Object] For Each obj In myList If i <= 0 Then i = myWidth Console.WriteLine() End If i -= 1 Console.Write("{0,8}", obj) Next obj Console.WriteLine() End Sub 'PrintValues End Class 'SamplesBitArray ' This code produces the following output. ' ' myBA1 ' Count: 5 ' Length: 5 ' Values: ' False False False False False ' myBA2 ' Count: 5 ' Length: 5 ' Values: ' False False False False False ' myBA3 ' Count: 40 ' Length: 40 ' Values: ' True False False False False False False False ' False True False False False False False False ' True True False False False False False False ' False False True False False False False False ' True False True False False False False False ' myBA4 ' Count: 5 ' Length: 5 ' Values: ' True False True True False ' myBA5 ' Count: 160 ' Length: 160 ' Values: ' False True True False False False False False ' False False False False False False False False ' False False False False False False False False ' False False False False False False False False ' True True True False False False False False ' False False False False False False False False ' False False False False False False False False ' False False False False False False False False ' False False False True False False False False ' False False False False False False False False ' False False False False False False False False ' False False False False False False False False ' True False False True False False False False ' False False False False False False False False ' False False False False False False False False ' False False False False False False False False ' False True False True False False False False ' False False False False False False False False ' False False False False False False False False ' False False False False False False False False
Public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
This implementation does not provide a synchronized (thread safe) wrapper for a BitArray.
Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads can still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.