C# - BitArray.Clone() Method with Example

In this tutorial, we will learn about the C# BitArray.Clone() method with its definition, usage, syntax, and example. By Nidhi Last updated : March 31, 2023

BitArray.Clone() Method

The BitArray.Clone() method is used to create a shallow copy of the current object of BitArray class.

Syntax

BitArray BitArray.Clone();

Parameter(s)

  • None

Return Value

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

C# Example of BitArray.Clone() Method

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

C# BitArray Class Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.