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.
Syntax:
public object Clone ();
Example:
using System;
using System.Collections;
public class GFG {
public static void Main(String[] args)
{
BitArray bit1 = new BitArray(4);
bit1[0] = false ;
bit1[1] = false ;
bit1[2] = true ;
bit1[3] = true ;
Console.WriteLine( "Elements of Original BitArray: \n" );
Result(bit1);
BitArray bit2 = (BitArray)bit1.Clone();
Console.WriteLine( "\nElements of Cloned BitArray: \n" );
Result(bit2);
Console.WriteLine( "\nReference Equals: {0}" ,
Object.ReferenceEquals(bit1, bit2));
}
public static void Result(IEnumerable bit)
{
foreach (Object obj in bit)
Console.WriteLine(obj);
}
}
|
Output:
Elements of Original BitArray:
False
False
True
True
Elements of Cloned BitArray:
False
False
True
True
Reference Equals: False