Skip to content
Related Articles

Related Articles

How to create a shallow copy of BitArray in C#

Improve Article
Save Article
  • Last Updated : 07 Feb, 2019
Improve Article
Save Article

BitArray.Clone() Method is used to create a shallow copy of the specified BitArray. A shallow copy of a collection copies only the elements of the collection irrespective of reference types or value types. But it does not copy the objects that the references refer to. The references in the new collection point to the same objects that the references in the original collection point to.


public object Clone ();


// C# code to illustrate the use 
// of BitArray.Clone Method
using System;
using System.Collections;
public class GFG {
    // Main Method
    public static void Main(String[] args)
        // Creating an empty BitArray
        BitArray bit1 = new BitArray(4);
        // Initializing values in bit1
        bit1[0] = false;
        bit1[1] = false;
        bit1[2] = true;
        bit1[3] = true;
        // Displaying the list
        Console.WriteLine("Elements of Original BitArray: \n");
        // calling function
        // using Clone() method
        BitArray bit2 = (BitArray)bit1.Clone();
        // Displaying the Cloned BitArray
        Console.WriteLine("\nElements of Cloned BitArray: \n");
        // calling function
        // checking for the equality  
        // of References bit1 and bit2 
        Console.WriteLine("\nReference Equals: {0}"
          Object.ReferenceEquals(bit1, bit2)); 
// method to display the values
public static void Result(IEnumerable bit) 
    // This method prints all the 
    // elements in the BitArray. 
    foreach(Object obj in bit) 


Elements of Original BitArray: 


Elements of Cloned BitArray: 


Reference Equals: False

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!