Q:

C# program to create a shallow copy of the BitArray (BitArray.Clone() Method)

belongs to collection: C# BitArray Class Programs

0

Syntax:

    BitArray BitArray.Clone();

Parameter(s):

  • None

Return value:

It returns a modified object that is a clone of the current object.

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

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 . . .

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# program to set the bit at a specific position i... >>
<< C# program to copy the entire BitArray to a compat...