# 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 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); ` `        ``} ` `    ``} ` `} `

Output:

```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);  ` `}  ` `} `

Output:

```Elements of Original ArrayList:

10
20
30
40
50

Elements of Cloned ArrayList:

10
20
30
40
50

10
20
30
40
50
60

10
20
30
40
50

Reference Equals: False
```

Reference:

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Article Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
{"ft_lang_label":"__label__en","ft_lang_prob":0.55381244,"math_prob":0.449071,"size":4649,"snap":"2020-45-2020-50","text_gpt3_token_len":1147,"char_repetition_ratio":0.18127018,"word_repetition_ratio":0.3158602,"special_character_ratio":0.26521832,"punctuation_ratio":0.13487242,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9585468,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-10-31T07:49:08Z\",\"WARC-Record-ID\":\"<urn:uuid:ce472d42-5ce5-4938-93a6-6607f9810dc4>\",\"Content-Length\":\"117159\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:dec2ed00-8ff7-46c0-96cb-2e7aa3a0b0ef>\",\"WARC-Concurrent-To\":\"<urn:uuid:c8e8d124-1e90-48ec-a722-47b27fbeafda>\",\"WARC-IP-Address\":\"23.48.39.225\",\"WARC-Target-URI\":\"https://www.geeksforgeeks.org/how-to-create-a-shallow-copy-of-arraylist-in-c-sharp/\",\"WARC-Payload-Digest\":\"sha1:JONARE7VKNSFE5G6EX5NCLVYGYEDRKH6\",\"WARC-Block-Digest\":\"sha1:A6UIP2LYC35BELXVJBYV2ZRMZII6NTIT\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-45/CC-MAIN-2020-45_segments_1603107916776.80_warc_CC-MAIN-20201031062721-20201031092721-00359.warc.gz\"}"}