The source code to create a shallow copy of the BitArray is given below. The given program is compiled and executed successfully.
using System;
using System.Collections;
class BitArrayEx
{
//Entry point of Program
static public void Main()
{
//Creation of BitArray objects
BitArray bitArr1 = new BitArray(5);
BitArray bitArr2;
int index = 0;
bitArr1[0] = true;
bitArr1[1] = false;
bitArr1[2] = true;
bitArr1[3] = false;
bitArr1[4] = true;
bitArr2 = (BitArray)bitArr1.Clone();
Console.WriteLine("Elements of BitArray1:");
for (index = 0; index < bitArr1.Length; index++)
{
Console.WriteLine("\tIndex " + index + ": " + bitArr1.Get(index));
}
Console.WriteLine("Elements of Clone of BitArray1 i.e. BitArray2:");
for (index = 0; index < bitArr2.Length; index++)
{
Console.WriteLine("\tIndex " + index + ": " + bitArr2.Get(index));
}
}
}
Output:
Elements of BitArray1:
Index 0: True
Index 1: False
Index 2: True
Index 3: False
Index 4: True
Elements of Clone of BitArray1 i.e. BitArray2:
Index 0: True
Index 1: False
Index 2: True
Index 3: False
Index 4: True
Press any key to continue . . .
Program:
The source code to create a shallow copy of the BitArray is given below. The given program is compiled and executed successfully.
Output: