[Serializable] |
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 could 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.
Indexes in this collection are zero-based.
using System; using System.Collections; public class SamplesBitArray { public static void Main() { // Creates and initializes several BitArrays. BitArray myBA1 = new BitArray( 5 ); BitArray myBA2 = new BitArray( 5, false ); byte[] myBytes = new byte[5] { 1, 2, 3, 4, 5 }; BitArray myBA3 = new BitArray( myBytes ); bool[] myBools = new bool[5] { true, false, true, true, false }; BitArray myBA4 = new BitArray( myBools ); int[] myInts = new int[5] { 6, 7, 8, 9, 10 }; BitArray myBA5 = new BitArray( myInts ); // Displays the properties and values of the BitArrays. Console.WriteLine( "myBA1" ); Console.WriteLine( "\tCount: {0}", myBA1.Count ); Console.WriteLine( "\tLength: {0}", myBA1.Length ); Console.WriteLine( "\tValues:" ); PrintValues( myBA1, 8 ); Console.WriteLine( "myBA2" ); Console.WriteLine( "\tCount: {0}", myBA2.Count ); Console.WriteLine( "\tLength: {0}", myBA2.Length ); Console.WriteLine( "\tValues:" ); PrintValues( myBA2, 8 ); Console.WriteLine( "myBA3" ); Console.WriteLine( "\tCount: {0}", myBA3.Count ); Console.WriteLine( "\tLength: {0}", myBA3.Length ); Console.WriteLine( "\tValues:" ); PrintValues( myBA3, 8 ); Console.WriteLine( "myBA4" ); Console.WriteLine( "\tCount: {0}", myBA4.Count ); Console.WriteLine( "\tLength: {0}", myBA4.Length ); Console.WriteLine( "\tValues:" ); PrintValues( myBA4, 8 ); Console.WriteLine( "myBA5" ); Console.WriteLine( "\tCount: {0}", myBA5.Count ); Console.WriteLine( "\tLength: {0}", myBA5.Length ); Console.WriteLine( "\tValues:" ); PrintValues( myBA5, 8 ); } public static void PrintValues( IEnumerable myList, int myWidth ) { System.Collections.IEnumerator myEnumerator = myList.GetEnumerator(); int i = myWidth; while ( myEnumerator.MoveNext() ) { if ( i <= 0 ) { i = myWidth; Console.WriteLine(); } i--; Console.Write( "\t{0}", myEnumerator.Current ); } Console.WriteLine(); } } /* 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 */
ctor #1 | Overloaded:.ctor(BitArray bits) Initializes a new instance of the BitArray class that contains bit values copied from the specified BitArray. |
ctor #2 | Overloaded:.ctor(bool[] values) Initializes a new instance of the BitArray class that contains bit values copied from the specified array of Booleans. |
ctor #3 | Overloaded:.ctor(byte[] bytes) Initializes a new instance of the BitArray class that contains bit values copied from the specified array of bytes. |
ctor #4 | Overloaded:.ctor(int length) Initializes a new instance of the BitArray class that can hold the specified number of bit values, which are initially set to false. |
ctor #5 | Overloaded:.ctor(int[] values) Initializes a new instance of the BitArray class that contains bit values copied from the specified array of 32-bit integers. |
ctor #6 | Overloaded:.ctor(int length, bool defaultValue) Initializes a new instance of the BitArray class that can hold the specified number of bit values, which are initially set to the specified value. |
Count | Read-only Gets the number of elements contained in the BitArray. |
IsReadOnly | Read-only Gets a value indicating whether the BitArray is read-only. |
IsSynchronized | Read-only Gets a value indicating whether access to the BitArray is synchronized (thread-safe). |
Item | Read-write Gets or sets the value of the bit at a specific position in the BitArray. |
Length | Read-write Gets or sets the number of elements in the BitArray. |
SyncRoot | Read-only Gets an object that can be used to synchronize access to the BitArray. |
And | Performs the bitwise AND operation on the elements in the current BitArray against the corresponding elements in the specified BitArray. |
Clone | Creates a shallow copy of the BitArray. |
CopyTo | Copies the entire BitArray to a compatible one-dimensional Array, starting at the specified index of the target array. |
Equals (inherited from System.Object) |
See base class member description: System.Object.Equals Derived from System.Object, the primary base class for all objects. |
Get | Gets the value of the bit at a specific position in the BitArray. |
GetEnumerator | Returns an enumerator that can iterate through the BitArray. |
GetHashCode (inherited from System.Object) |
See base class member description: System.Object.GetHashCode Derived from System.Object, the primary base class for all objects. |
GetType (inherited from System.Object) |
See base class member description: System.Object.GetType Derived from System.Object, the primary base class for all objects. |
Not | Inverts all the bit values in the current BitArray, so that elements set to true are changed to false, and elements set to false are changed to true. |
Or | Performs the bitwise OR operation on the elements in the current BitArray against the corresponding elements in the specified BitArray. |
Set | Sets the bit at a specific position in the BitArray to the specified value. |
SetAll | Sets all bits in the BitArray to the specified value. |
ToString (inherited from System.Object) |
See base class member description: System.Object.ToString Derived from System.Object, the primary base class for all objects. |
Xor | Performs the bitwise eXclusive OR operation on the elements in the current BitArray against the corresponding elements in the specified BitArray. |
Finalize (inherited from System.Object) |
See base class member description: System.Object.Finalize Derived from System.Object, the primary base class for all objects. |
MemberwiseClone (inherited from System.Object) |
See base class member description: System.Object.MemberwiseClone Derived from System.Object, the primary base class for all objects. |
Hierarchy:
public BitArray(BitArray( |
bits
Exception Type | Condition |
---|---|
ArgumentNullException | bits is null. |
public BitArray( |
values
Exception Type | Condition |
---|---|
ArgumentNullException | values is null. |
public BitArray( |
bytes
Exception Type | Condition |
---|---|
ArgumentNullException | bytes is null. |
public BitArray( |
length
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | length is less than zero. |
public BitArray( |
values
Exception Type | Condition |
---|---|
ArgumentNullException | values is null. |
length
defaultValue
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | length is less than zero. |
public int Count {get;}
|
public bool IsReadOnly {get;}
|
A collection that is read-only does not allow the addition, removal, or modification of elements after the collection is created.
public bool IsSynchronized {get;}
|
Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads could 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.
The following code example shows how to lock the collection using the BitArray.SyncRoot during the entire enumeration:
BitArray myCollection = new BitArray(); lock( myCollection.SyncRoot ) { foreach ( Object item in myCollection ) { // Insert your code here. } }
public bool this[int index] {get; set;}
|
index
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | index is less than zero. -or- index is equal to or greater than BitArray.Count. |
myCollection[index]
.
public int Length {get; set;}
|
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | The property is set to a value that is less than zero. |
If BitArray.Length is set to a value that is less than BitArray.Count, the BitArray is truncated and the elements after the index value -1 are deleted.
If BitArray.Length is set to a value that is greater than BitArray.Count, the new elements are set to false.
public object SyncRoot {get;}
|
Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads could 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.
The following code example shows how to lock the collection using the BitArray.SyncRoot during the entire enumeration:
BitArray myCollection = new BitArray(); lock( myCollection.SyncRoot ) { foreach ( Object item in myCollection ) { // Insert your code here. } }
value
Exception Type | Condition |
---|---|
ArgumentNullException | value is null. |
ArgumentException | value and the current BitArray do not have the same number of elements. |
using System; using System.Collections; public class SamplesBitArray { public static void Main() { // Creates and initializes two BitArrays of the same size. BitArray myBA1 = new BitArray( 4 ); BitArray myBA2 = new BitArray( 4 ); myBA1[0] = myBA1[1] = false; myBA1[2] = myBA1[3] = true; myBA2[0] = myBA2[2] = false; myBA2[1] = myBA2[3] = true; // Performs a bitwise AND operation between BitArray instances of the same size. Console.WriteLine( "Initial values" ); Console.Write( "myBA1:" ); PrintValues( myBA1, 8 ); Console.Write( "myBA2:" ); PrintValues( myBA2, 8 ); Console.WriteLine(); Console.WriteLine( "Result" ); Console.Write( "AND:" ); PrintValues( myBA1.And( myBA2 ), 8 ); Console.WriteLine(); Console.WriteLine( "After AND" ); Console.Write( "myBA1:" ); PrintValues( myBA1, 8 ); Console.Write( "myBA2:" ); PrintValues( myBA2, 8 ); Console.WriteLine(); // Performing AND between BitArray instances of different sizes returns an exception. try { BitArray myBA3 = new BitArray( 8 ); myBA3[0] = myBA3[1] = myBA3[2] = myBA3[3] = false; myBA3[4] = myBA3[5] = myBA3[6] = myBA3[7] = true; myBA1.And( myBA3 ); } catch ( Exception myException ) { Console.WriteLine("Exception: " + myException.ToString()); } } public static void PrintValues( IEnumerable myList, int myWidth ) { System.Collections.IEnumerator myEnumerator = myList.GetEnumerator(); int i = myWidth; while ( myEnumerator.MoveNext() ) { if ( i <= 0 ) { i = myWidth; Console.WriteLine(); } i--; Console.Write( "\t{0}", myEnumerator.Current ); } Console.WriteLine(); } } /* This code produces the following output. Initial values myBA1: False False True True myBA2: False True False True Result AND: False False False True After AND myBA1: False False False True myBA2: False True False True Exception: System.ArgumentException: Array lengths must be the same. at System.Collections.BitArray.And(BitArray value) in <UnknownName>:line 0 at SamplesBitArray.Main() */
public object Clone(); |
In contrast, a deep copy of a collection copies the elements and everything directly or indirectly referenced by the elements.
array
index
Exception Type | Condition |
---|---|
ArgumentNullException | array is null. |
ArgumentOutOfRangeException | index is less than zero. |
ArgumentException | array is multidimensional. -or- index is equal to or greater than the length of array. -or- The number of elements in the source BitArray is greater than the available space from index to the end of the destination array. |
InvalidCastException | The type of the source BitArray cannot be cast automatically to the type of the destination array. |
This method uses Array.Copy to copy the elements.
using System; using System.Collections; public class SamplesBitArray { public static void Main() { // Creates and initializes the source BitArray. BitArray myBA = new BitArray( 4 ); myBA[0] = myBA[1] = myBA[2] = myBA[3] = true; // Creates and initializes the one-dimensional target Array of type Boolean. Array myBoolArray = Array.CreateInstance( typeof(bool), 8 ); myBoolArray.SetValue( false, 0 ); myBoolArray.SetValue( false, 1 ); // Displays the values of the target Array. Console.WriteLine( "The target Boolean Array contains the following (before and after copying):" ); PrintValues( myBoolArray, '\t' ); // Copies the entire source BitArray to the target BitArray, starting at index 3. myBA.CopyTo( myBoolArray, 3 ); // Displays the values of the target Array. PrintValues( myBoolArray, '\t' ); // Creates and initializes the one-dimensional target Array of type integer. Array myIntArray = Array.CreateInstance( typeof(int), 8 ); myIntArray.SetValue( 42, 0 ); myIntArray.SetValue( 43, 1 ); // Displays the values of the target Array. Console.WriteLine( "The target Boolean Array contains the following (before and after copying):" ); PrintValues( myIntArray, '\t' ); // Copies the entire source BitArray to the target BitArray, starting at index 3. myBA.CopyTo( myIntArray, 3 ); // Displays the values of the target Array. PrintValues( myIntArray, '\t' ); // Creates and initializes the one-dimensional target Array of type integer. Array myByteArray = Array.CreateInstance( typeof(byte), 8 ); myByteArray.SetValue( (byte) 10, 0 ); myByteArray.SetValue( (byte) 11, 1 ); // Displays the values of the target Array. Console.WriteLine( "The target Boolean Array contains the following (before and after copying):" ); PrintValues( myByteArray, '\t' ); // Copies the entire source BitArray to the target BitArray, starting at index 3. myBA.CopyTo( myByteArray, 3 ); // Displays the values of the target Array. PrintValues( myByteArray, '\t' ); // Returns an exception if the array is not of type Boolean, integer or byte. try { Array myStringArray=Array.CreateInstance( typeof(String), 8 ); myStringArray.SetValue( "Hello", 0 ); myStringArray.SetValue( "World", 1 ); myBA.CopyTo( myStringArray, 3 ); } catch ( Exception myException ) { Console.WriteLine("Exception: " + myException.ToString()); } } public static void PrintValues( Array myArr, char mySeparator ) { System.Collections.IEnumerator myEnumerator = myArr.GetEnumerator(); int i = 0; int cols = myArr.GetLength( myArr.Rank - 1 ); while ( myEnumerator.MoveNext() ) { if ( i < cols ) { i++; } else { Console.WriteLine(); i = 1; } Console.Write( "{0}{1}", mySeparator, myEnumerator.Current ); } Console.WriteLine(); } } /* This code produces the following output. The target Boolean Array contains the following (before and after copying): False False False False False False False False False False False True True True True False The target Boolean Array contains the following (before and after copying): 42 43 0 0 0 0 0 0 42 43 0 15 0 0 0 0 The target Boolean Array contains the following (before and after copying): 10 11 0 0 0 0 0 0 10 11 0 15 0 0 0 0 Exception: System.ArgumentException: Only supported copyto on bit arrays are bool[], int[] and byte[]. at System.Collections.BitArray.CopyTo(Array array, Int32 index) in <UnknownName>:line 0 at SamplesBitArray.Main() */
~BitArray(); |
index
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | index is less than zero. -or- index is greater than or equal to the number of elements in the BitArray. |
using System; using System.Collections; public class SamplesBitArray { public static void Main() { // Creates and initializes a BitArray. BitArray myBA = new BitArray( 5 ); // Displays the properties and values of the BitArray. Console.WriteLine( "myBA values:" ); PrintIndexAndValues( myBA ); // Sets all the elements to true. myBA.SetAll( true ); // Displays the properties and values of the BitArray. Console.WriteLine( "After setting all elements to true," ); PrintIndexAndValues( myBA ); // Sets the last index to false. myBA.Set( myBA.Count - 1, false ); // Displays the properties and values of the BitArray. Console.WriteLine( "After setting the last element to false," ); PrintIndexAndValues( myBA ); // Gets the value of the last two elements. Console.WriteLine( "The last two elements are: " ); Console.WriteLine( "\tat index {0} : {1}", myBA.Count - 2, myBA.Get( myBA.Count - 2 ) ); Console.WriteLine( "\tat index {0} : {1}", myBA.Count - 1, myBA.Get( myBA.Count - 1 ) ); } public static void PrintIndexAndValues( IEnumerable myList ) { int i = 0; System.Collections.IEnumerator myEnumerator = myList.GetEnumerator(); while ( myEnumerator.MoveNext() ) Console.WriteLine( "\t[{0}]:\t{1}", i++, myEnumerator.Current ); Console.WriteLine(); } } /* This code produces the following output. myBA values: [0]: False [1]: False [2]: False [3]: False [4]: False After setting all elements to true, [0]: True [1]: True [2]: True [3]: True [4]: True After setting the last element to false, [0]: True [1]: True [2]: True [3]: True [4]: False The last two elements are: at index 3 : True at index 4 : False */
public IEnumerator GetEnumerator(); |
Initially, the enumerator is positioned before the first element in the collection. IEnumerator.Reset also brings the enumerator back to this position. At this position, calling IEnumerator.Current throws an exception. Therefore, you must call IEnumerator.MoveNext to advance the enumerator to the first element of the collection before reading the value of IEnumerator.Current.
IEnumerator.Current returns the same object until either IEnumerator.MoveNext or IEnumerator.Reset is called. IEnumerator.MoveNext sets IEnumerator.Current to the next element.
After the end of the collection is passed, the enumerator is positioned after the last element in the collection, and calling IEnumerator.MoveNext returns false. If the last call to IEnumerator.MoveNext returned false, calling IEnumerator.Current throws an exception. To set IEnumerator.Current to the first element of the collection again, you can call IEnumerator.Reset followed by IEnumerator.MoveNext.
An enumerator remains valid as long as the collection remains unchanged. If changes are made to the collection, such as adding, modifying or deleting elements, the enumerator is irrecoverably invalidated and the next call to IEnumerator.MoveNext or IEnumerator.Reset throws an InvalidOperationException. If the collection is modified between IEnumerator.MoveNext and IEnumerator.Current, IEnumerator.Current will return the element that it is set to, even if the enumerator is already invalidated.
The enumerator does not have exclusive access to the collection; therefore, enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads could 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.
public virtual int GetHashCode(); |
public Type GetType(); |
protected object MemberwiseClone(); |
public BitArray Not(); |
using System; using System.Collections; public class SamplesBitArray { public static void Main() { // Creates and initializes two BitArrays of the same size. BitArray myBA1 = new BitArray( 4 ); BitArray myBA2 = new BitArray( 4 ); myBA1[0] = myBA1[1] = false; myBA1[2] = myBA1[3] = true; myBA2[0] = myBA2[2] = false; myBA2[1] = myBA2[3] = true; // Performs a bitwise NOT operation between BitArray instances of the same size. Console.WriteLine( "Initial values" ); Console.Write( "myBA1:" ); PrintValues( myBA1, 8 ); Console.Write( "myBA2:" ); PrintValues( myBA2, 8 ); Console.WriteLine(); myBA1.Not(); myBA2.Not(); Console.WriteLine( "After NOT" ); Console.Write( "myBA1:" ); PrintValues( myBA1, 8 ); Console.Write( "myBA2:" ); PrintValues( myBA2, 8 ); Console.WriteLine(); } public static void PrintValues( IEnumerable myList, int myWidth ) { System.Collections.IEnumerator myEnumerator = myList.GetEnumerator(); int i = myWidth; while ( myEnumerator.MoveNext() ) { if ( i <= 0 ) { i = myWidth; Console.WriteLine(); } i--; Console.Write( "\t{0}", myEnumerator.Current ); } Console.WriteLine(); } } /* This code produces the following output. Initial values myBA1: False False True True myBA2: False True False True After NOT myBA1: True True False False myBA2: True False True False */
value
Exception Type | Condition |
---|---|
ArgumentNullException | value is null. |
ArgumentException | value and the current BitArray do not have the same number of elements. |
using System; using System.Collections; public class SamplesBitArray { public static void Main() { // Creates and initializes two BitArrays of the same size. BitArray myBA1 = new BitArray( 4 ); BitArray myBA2 = new BitArray( 4 ); myBA1[0] = myBA1[1] = false; myBA1[2] = myBA1[3] = true; myBA2[0] = myBA2[2] = false; myBA2[1] = myBA2[3] = true; // Performs a bitwise OR operation between BitArray instances of the same size. Console.WriteLine( "Initial values" ); Console.Write( "myBA1:" ); PrintValues( myBA1, 8 ); Console.Write( "myBA2:" ); PrintValues( myBA2, 8 ); Console.WriteLine(); Console.WriteLine( "Result" ); Console.Write( "OR:" ); PrintValues( myBA1.Or( myBA2 ), 8 ); Console.WriteLine(); Console.WriteLine( "After OR" ); Console.Write( "myBA1:" ); PrintValues( myBA1, 8 ); Console.Write( "myBA2:" ); PrintValues( myBA2, 8 ); Console.WriteLine(); // Performing OR between BitArray instances of different sizes returns an exception. try { BitArray myBA3 = new BitArray( 8 ); myBA3[0] = myBA3[1] = myBA3[2] = myBA3[3] = false; myBA3[4] = myBA3[5] = myBA3[6] = myBA3[7] = true; myBA1.Or( myBA3 ); } catch ( Exception myException ) { Console.WriteLine("Exception: " + myException.ToString()); } } public static void PrintValues( IEnumerable myList, int myWidth ) { System.Collections.IEnumerator myEnumerator = myList.GetEnumerator(); int i = myWidth; while ( myEnumerator.MoveNext() ) { if ( i <= 0 ) { i = myWidth; Console.WriteLine(); } i--; Console.Write( "\t{0}", myEnumerator.Current ); } Console.WriteLine(); } } /* This code produces the following output. Initial values myBA1: False False True True myBA2: False True False True Result OR: False True True True After OR myBA1: False True True True myBA2: False True False True Exception: System.ArgumentException: Array lengths must be the same. at System.Collections.BitArray.Or(BitArray value) in <UnknownName>:line 0 at SamplesBitArray.Main() */
index
value
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | index is less than zero. -or- index is greater than or equal to the number of elements in the BitArray. |
using System; using System.Collections; public class SamplesBitArray { public static void Main() { // Creates and initializes a BitArray. BitArray myBA = new BitArray( 5 ); // Displays the properties and values of the BitArray. Console.WriteLine( "myBA values:" ); PrintIndexAndValues( myBA ); // Sets all the elements to true. myBA.SetAll( true ); // Displays the properties and values of the BitArray. Console.WriteLine( "After setting all elements to true," ); PrintIndexAndValues( myBA ); // Sets the last index to false. myBA.Set( myBA.Count - 1, false ); // Displays the properties and values of the BitArray. Console.WriteLine( "After setting the last element to false," ); PrintIndexAndValues( myBA ); // Gets the value of the last two elements. Console.WriteLine( "The last two elements are: " ); Console.WriteLine( "\tat index {0} : {1}", myBA.Count - 2, myBA.Get( myBA.Count - 2 ) ); Console.WriteLine( "\tat index {0} : {1}", myBA.Count - 1, myBA.Get( myBA.Count - 1 ) ); } public static void PrintIndexAndValues( IEnumerable myList ) { int i = 0; System.Collections.IEnumerator myEnumerator = myList.GetEnumerator(); while ( myEnumerator.MoveNext() ) Console.WriteLine( "\t[{0}]:\t{1}", i++, myEnumerator.Current ); Console.WriteLine(); } } /* This code produces the following output. myBA values: [0]: False [1]: False [2]: False [3]: False [4]: False After setting all elements to true, [0]: True [1]: True [2]: True [3]: True [4]: True After setting the last element to false, [0]: True [1]: True [2]: True [3]: True [4]: False The last two elements are: at index 3 : True at index 4 : False */
public void SetAll( |
value
using System; using System.Collections; public class SamplesBitArray { public static void Main() { // Creates and initializes a BitArray. BitArray myBA = new BitArray( 5 ); // Displays the properties and values of the BitArray. Console.WriteLine( "myBA values:" ); PrintIndexAndValues( myBA ); // Sets all the elements to true. myBA.SetAll( true ); // Displays the properties and values of the BitArray. Console.WriteLine( "After setting all elements to true," ); PrintIndexAndValues( myBA ); // Sets the last index to false. myBA.Set( myBA.Count - 1, false ); // Displays the properties and values of the BitArray. Console.WriteLine( "After setting the last element to false," ); PrintIndexAndValues( myBA ); // Gets the value of the last two elements. Console.WriteLine( "The last two elements are: " ); Console.WriteLine( "\tat index {0} : {1}", myBA.Count - 2, myBA.Get( myBA.Count - 2 ) ); Console.WriteLine( "\tat index {0} : {1}", myBA.Count - 1, myBA.Get( myBA.Count - 1 ) ); } public static void PrintIndexAndValues( IEnumerable myList ) { int i = 0; System.Collections.IEnumerator myEnumerator = myList.GetEnumerator(); while ( myEnumerator.MoveNext() ) Console.WriteLine( "\t[{0}]:\t{1}", i++, myEnumerator.Current ); Console.WriteLine(); } } /* This code produces the following output. myBA values: [0]: False [1]: False [2]: False [3]: False [4]: False After setting all elements to true, [0]: True [1]: True [2]: True [3]: True [4]: True After setting the last element to false, [0]: True [1]: True [2]: True [3]: True [4]: False The last two elements are: at index 3 : True at index 4 : False */
public virtual string ToString(); |
value
Exception Type | Condition |
---|---|
ArgumentNullException | value is null. |
ArgumentException | value and the current BitArray do not have the same number of elements. |
using System; using System.Collections; public class SamplesBitArray { public static void Main() { // Creates and initializes two BitArrays of the same size. BitArray myBA1 = new BitArray( 4 ); BitArray myBA2 = new BitArray( 4 ); myBA1[0] = myBA1[1] = false; myBA1[2] = myBA1[3] = true; myBA2[0] = myBA2[2] = false; myBA2[1] = myBA2[3] = true; // Performs a bitwise XOR operation between BitArray instances of the same size. Console.WriteLine( "Initial values" ); Console.Write( "myBA1:" ); PrintValues( myBA1, 8 ); Console.Write( "myBA2:" ); PrintValues( myBA2, 8 ); Console.WriteLine(); Console.WriteLine( "Result" ); Console.Write( "XOR:" ); PrintValues( myBA1.Xor( myBA2 ), 8 ); Console.WriteLine(); Console.WriteLine( "After XOR" ); Console.Write( "myBA1:" ); PrintValues( myBA1, 8 ); Console.Write( "myBA2:" ); PrintValues( myBA2, 8 ); Console.WriteLine(); // Performing XOR between BitArray instances of different sizes returns an exception. try { BitArray myBA3 = new BitArray( 8 ); myBA3[0] = myBA3[1] = myBA3[2] = myBA3[3] = false; myBA3[4] = myBA3[5] = myBA3[6] = myBA3[7] = true; myBA1.Xor( myBA3 ); } catch ( Exception myException ) { Console.WriteLine("Exception: " + myException.ToString()); } } public static void PrintValues( IEnumerable myList, int myWidth ) { System.Collections.IEnumerator myEnumerator = myList.GetEnumerator(); int i = myWidth; while ( myEnumerator.MoveNext() ) { if ( i <= 0 ) { i = myWidth; Console.WriteLine(); } i--; Console.Write( "\t{0}", myEnumerator.Current ); } Console.WriteLine(); } } /* This code produces the following output. Initial values myBA1: False False True True myBA2: False True False True Result XOR: False True True False After XOR myBA1: False True True False myBA2: False True False True Exception: System.ArgumentException: Array lengths must be the same. at System.Collections.BitArray.Xor(BitArray value) in <UnknownName>:line 0 at SamplesBitArray.Main() */