C# ArrayList - Clone() Method



The C# ArrayList Clone() method is used to create the shallow copy of the ArrayList.

A shallow copy of a collection copies only the elements of the collection, whether they are reference types or value types, but it does not copy the objects that the reference refers to.

Syntax

Following is the syntax of the C# ArrayList Clone() method −

public virtual object Clone ();

Parameters

This method does not accepts any parameters.

Return value

This method returns a shallow copy of the ArrayList.

Example 1: Shallow Copy of ArrayList

Following is the basic example of the Clone() method to create a shallow copy of an ArrayList −

    
using System;
using System.Collections;
class Program
{
   static void Main()
   {
      ArrayList arrayList = new ArrayList { "A", "B", "C", "D" };
      
      Console.WriteLine("Before Clone: ");
      foreach (var item in arrayList)
      {
         Console.Write(item + " ");
      }
      
      ArrayList newArrayList = (ArrayList)arrayList.Clone();
      
      Console.WriteLine("\nAfter Clone:");
      foreach (var item in newArrayList)
      {
         Console.Write(item + " ");
      }
   }
}

Output

Following is the output −

Before Clone: 
A B C D 
After Clone:
A B C D 

Example 2: Cloning an ArrayList of Custom Person Objects

Let's see another example, here we clone an ArrayList of custom Person objects. Each person is defined by a Name and Age. The Clone() method is used to create a shallow copy of the ArrayList −

using System;
using System.Collections;
public class Person
{
   public string Name { get; set; }
   public int Age { get; set; }
   
   public Person(string name, int age)
   {
      Name = name;
      Age = age;
   }
   
   public override string ToString()
   {
      return $"Name: {Name}, Age: {Age}";
   }
}
class Program
{
   static void Main()
   {
      ArrayList personList = new ArrayList
      {
         new Person("Akash", 30),
         new Person("Rahul", 25),
         new Person("Sudeep", 28)
      };
      
      Console.WriteLine("Before Clone: ");
      foreach (var person in personList)
      {
         Console.Write(person + " | ");
      }
      
      ArrayList clonedPersonList = (ArrayList)personList.Clone();
      
      Console.WriteLine("\nAfter Clone:");
      foreach (var person in clonedPersonList)
      {
         Console.Write(person + " | ");
      }
   }
}

Output

Following is the output −

Before Clone: 
Name: Akash, Age: 30 | Name: Rahul, Age: 25 | Name: Sudeep, Age: 28 | 
After Clone:
Name: Akash, Age: 30 | Name: Rahul, Age: 25 | Name: Sudeep, Age: 28 | 

Example 3: Creating a Shallow Copy of an ArrayList

The below example creates an ArrayList of integers. We then use the clone() method to create a shallow copy of the ArrayList and then display the elements of both original and clone lists −

using System;
using System.Collections;

class Program
{
   static void Main()
   {
      ArrayList intList = new ArrayList { 1, 2, 3, 4, 5 };
      
      Console.WriteLine("Before Clone: ");
      foreach (var number in intList)
      {
        Console.Write(number + " ");
      }
      
      ArrayList clonedIntList = (ArrayList)intList.Clone();
      
      Console.WriteLine("\nAfter Clone:");
      foreach (var number in clonedIntList)
      {
        Console.Write(number + " ");
      }
   }
}

Output

Following is the output −

Before Clone: 
1 2 3 4 5 
After Clone:
1 2 3 4 5 
csharp_arraylist.htm
Advertisements