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 collection point to the same objects that the references in the original collection point to.
Syntax:
public virtual object Clone ();
Below programs illustrate the use of above-discussed method:
Example 1:
// C# code to illustrate the use // of ArrayList.Clone Method using System; using System.Collections; public class GFG { // Main Method public static void Main(String[] args) { // Creating an empty ArrayList ArrayList list = new ArrayList(); // Use Add() method // to Add elements // in the list list.Add("Geeks"); list.Add("for"); list.Add("Geeks"); list.Add("10"); list.Add("20"); // Displaying the list Console.WriteLine("Elements of Original ArrayList: \n"); // Displaying the elements in ArrayList foreach(string str in list) { Console.WriteLine(str); } // Creating another ArrayList ArrayList sec_list = new ArrayList(); // using Clone() Method sec_list = (ArrayList)list.Clone(); // Displaying the Cloned ArrayList Console.WriteLine("\nElements of Cloned ArrayList: \n"); // Displaying the elements in ArrayList foreach(string str1 in sec_list) { Console.WriteLine(str1); } } } |
Elements of Original ArrayList: Geeks for Geeks 10 20 Elements of Cloned ArrayList: Geeks for Geeks 10 20
Example 2:
// C# code to illustrate the use // of ArrayList.Clone Method using System; using System.Collections; public class GFG { // Main Method public static void Main(String[] args) { // Creating an empty ArrayList ArrayList list = new ArrayList(); // Use Add() method // to Add elements // in the list list.Add(10); list.Add(20); list.Add(30); list.Add(40); list.Add(50); // Displaying the list Console.WriteLine("Elements of Original ArrayList: \n"); // calling function Result(list); // using Clone() method ArrayList sec_list = (ArrayList)list.Clone(); // Displaying the Cloned ArrayList Console.WriteLine("\nElements of Cloned ArrayList: \n"); // calling function Result(sec_list); // adding a new value to // original ArrayList list.Add(60); Console.WriteLine("\nAfter Adding, Original ArrayList: \n"); // Calling function Result(list); Console.WriteLine("\nAfter Adding, Cloned ArrayList: \n"); // Calling function Result(sec_list); // checking for the equality // of References list and sec_list Console.WriteLine("\nReference Equals: {0}", Object.ReferenceEquals(list, sec_list)); } // method to display the values public static void Result(ArrayList ar) { // This method prints all the // elements in the ArrayList. foreach(int i in ar) Console.WriteLine(i); } } |
Elements of Original ArrayList: 10 20 30 40 50 Elements of Cloned ArrayList: 10 20 30 40 50 After Adding, Original ArrayList: 10 20 30 40 50 60 After Adding, Cloned ArrayList: 10 20 30 40 50 Reference Equals: False
Reference:
Recommended Posts:
- Shallow Copy and Deep Copy in C#
- How to create a shallow copy of SortedList Object in C#
- How to create a shallow copy of Hashtable in C#
- How to create a shallow copy of BitArray in C#
- C# | How to create a shallow copy of the BitArray
- C# | Copy the elements of collection over a range of elements in ArrayList
- C# | How to copy the entire ArrayList to a one-dimensional Array
- How to create the ArrayList in C#
- C# | Remove the first occurrence of a specific object from the ArrayList
- C# | Get the number of elements actually contained in the ArrayList
- C# | Check whether an element is contained in the ArrayList
- C# | Check if the ArrayList is read-only
- C# | Insert an element into the ArrayList at the specified index
- C# | Add an object to the end of the ArrayList
- C# | Copying the elements of ArrayList to a new array
- C# | Get or set the number of elements that the ArrayList can contain
- C# | Remove a range of elements from the ArrayList
- C# | Check if the ArrayList has a fixed size
- C# | Remove all elements from the ArrayList
- C# | Remove the element at the specified index of the ArrayList
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

