Clone method in ArrayList

In this article I will discuss the Clone method of ArrayList.
  • 2882

Introduction

Clone method of ArrayList creates a shallow copy of the ArrayList, means this Clone method simply create the copy of all element of the ArrayList.

Example

public class MyArrayList

{

    public static void Main()

    {

        // Creates a new ArrayList.

        ArrayList myArray = new ArrayList();

        myArray.Add("one");

        myArray.Add("two");

        myArray.Add("three");

        myArray.Add("four");

        myArray.Add("five");

        myArray.Add("six");

        myArray.Add("seven");

        myArray.Add("eight");

        myArray.Add("nine");

        myArray.Add("ten");

        // Displays the values of the ArrayList.

        Console.WriteLine("The ArrayList contains the following values:");

        foreach (string s in myArray)

        {

            Console.WriteLine(s);

        }

        Console.WriteLine("The shallow copy of arraylist is as:");

        myArray.Clone();

        foreach (string s in myArray)

        {

            Console.WriteLine(s);

        }

    }

}


Output

clone.jpg 

 

Ask Your Question 
 
Got a programming related question? You may want to post your question here
 

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.