C# | How to create a shallow copy of the BitArray Last Updated : 16 Jul, 2021 Comments Improve Suggest changes Like Article Like Report BitArray.Clone Method is used to create a shallow copy of the BitArray. This method only copies the elements of the collection, whether they are reference types or value types, but it does not copy the objects that the references refer to.Syntax: public object Clone (); Example 1: Here we create an array of type byte. Then declare an object B1 of type BitArray and initialize it using the byte array created before. Declare another object B2 of type BitArray which will be used to store the clone of B1. The instruction B1.Clone returns the shallow copy of B1, which is of type System.Collections. Therefore explicitly convert it to BitArray before storing it in B2 object. And finally, display the cloned BitArray. csharp // C# program to demonstrate the // BitArray.Clone Method using System; using System.Collections; class GFG { static void Main(string[] args) { // Declaring a byte array byte[] ByteArray = new byte[] {1, 3}; // Declaring a BitArray object // Initializing to the byte array BitArray B1 = new BitArray(ByteArray); // Declaring a new BitArray object BitArray B2 = new BitArray(4); // Using the BitArray.Clone method B2 = (BitArray)B1.Clone(); // Displaying the length of the BitArray Console.WriteLine("Length of B2: {0}", B2.Length); Console.WriteLine("\nB2 Contains:"); // To display the values // of BitArray object B2 foreach(Object item in B2) { Console.WriteLine(item); } } } Output: Length of B2: 16 B2 Contains: True False False False False False False False True True False False False False False False Example 2: A shallow copy only copies the contents of the BitArray and not the object references, any changes made to B2 will only update those changes in B2 and not in B1. csharp // C#Program to show changes in clone // don't affect the original BitArray using System; using System.Collections; class GFG { static void Main(string[] args) { // Declaring a bool array bool[] BooleanArray = new bool[] {true, false, true, false }; // Declaring an object B1 of BitArray // Initialising with the bool array BitArray B1 = new BitArray(BooleanArray); int i; // Declaring object B2 of BitArray BitArray B2 = new BitArray(4); // Using the BitArray.Clone method B2 = (BitArray)B1.Clone(); i = 4; // Displaying elements of B2 Console.WriteLine("B2 Before Making any changes:"); foreach(Object item in B2) { if (i <= 0) { Console.WriteLine(); i = 6; } i--; Console.Write("{0, 4} ", item); } // Updating elements of B2 // at index 0 and 1 B2[0] = false; B2[1] = true; i = 4; // Displaying elements // of B2 after updating Console.WriteLine("\n\nB2 After changes:"); foreach(Object item in B2) { if (i <= 0) { Console.WriteLine(); i = 6; } i--; Console.Write("{0, 4} ", item); } // Displaying elements of B1 Console.WriteLine("\n\nB1 After Changes:"); i = 4; foreach(Object item in B1) { if (i <= 0) { Console.WriteLine(); i = 6; } i--; Console.Write("{0, 4} ", item); } } } Output: B2 Before Making any changes: True False True False B2 After changes: False True True False B1 After Changes: True False True False Reference: https://docs.microsoft.com/en-us/dotnet/api/system.collections.bitarray.clone?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | How to create a shallow copy of the BitArray M ManasiKirloskar Follow Improve Article Tags : C# CSharp-method CSharp-Collections-Namespace CSharp-Collections-BitArray Similar Reads How to create a shallow copy of BitArray in C# 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 colle 2 min read How to create a shallow copy of ArrayList in C# ArrayList.Clone() Method is used to create a shallow copy of the specified ArrayList. 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 col 3 min read C# | Copy the Stack to an Array Stack<T>.CopyTo(T[], Int32) Method is used to copy the Stack<T> to an existing 1-D Array which starts from the specified array index. Properties: The capacity of a Stack<T>is the number of elements the Stack<T> can hold. As elements are added to a Stack<T> , the capacit 2 min read C# | Copying the Hashtable elements to an Array Instance Hashtable.CopyTo(Array, Int32) Method is used to copy the elements of a Hashtable to a one-dimensional Array instance at the specified index.Syntax: public virtual void CopyTo (Array array, int arrayIndex); Parameters: array : The one-dimensional Array that is the destination of the DictionaryEntry 3 min read C# | How to copy the entire ArrayList to a one-dimensional Array ArrayList.CopyTo Method is used to copy the entire ArrayList to a compatible one-dimensional Array, starting at the beginning of the target array. Syntax: public virtual void CopyTo (Array array); Here, array is the one-dimensional Array which is the destination of the elements copied from ArrayList 3 min read Like