---
title: "How to copy a generic list in c#?"  
description: "How to copy a generic list in c#?"  
author: "Anupam Mishra"  
published: 2016-01-11  
updated: 2016-01-12  
canonical: https://www.mindstick.com/forum/33852/how-to-copy-a-generic-list-in-c-sharp  
category: "c#"  
tags: ["c#", ".net"]  
reading_time: 3 minutes  

---

# How to copy a generic list in c#?

I have two [generic lists](https://www.mindstick.com/forum/55127/how-to-return-multiple-generic-lists-from-a-method-using-entity-framework) and I want to [copy](https://www.mindstick.com/forum/161993/explain-the-numpy-array-copy-vs-view-with-example) the contents of the first one to the [second](https://answers.mindstick.com/qa/41761/how-did-the-second-industrial-revolution-influence-women-s-roles-in-society) and then modify either list without affecting the other one [and google](https://www.mindstick.com/news/3597/amazon-jumped-in-the-competition-with-microsoft-after-launching-its-new-ai-chip) hasn't been of much help since none of the proposed [answers](https://www.mindstick.com/forum/156899/salesforce-marketing-cloud-interview-questions-answers) worked in my case.I eventually got it working but the way I did it doesn't seem right. \
I hope a seasoned programmer can take take a look at my [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) and point out the proper way to do it or suggest a better solution to this admittedly silly problem.

```
class Program{    static void Main(string[] args)    {        // Making it readonly doesn't even compile.        List<Cat> firstList = new List<Cat>        {            new Cat {Name = "Indian", Breed = "Persian", Age = 15},            new Cat {Name = "Spenish", Breed = "Shy", Age = 17}        };        //   Doesn't work.        List<Cat> secondList = new List<Cat>(firstList);        //  Doesn't work.        List<Cat> secondList = firstList.ToList();        //  Doesn't work.        List<Cat> secondList = new List<Cat>();        secondList.AddRange(firstList);        //  Doesn't work.        List<Cat> secondList = new List<Cat>();        secondList = firstList.GetRange(0, firstList.Count);        //  Doesn't work.        Cat[] secondList = new Cat[2];        firstList.CopyTo(secondList);        // Doesn't work.        Cat[] secondList = new Cat[2];        var thirdList = firstList.ToArray().Clone();        secondList = (Cat[])thirdList;        List<Cat> secondList = new List<Cat>();        foreach (var cat in firstList)        {            secondList.Add(new Cat             {                 Name = cat.Name,                 Breed = cat.Breed,                 Age = cat.Age             });        }        Console.WriteLine(firstList[1].Age);        Console.WriteLine(secondList[1].Age);        secondList[1].Age++;        Console.WriteLine(firstList[1].Age);        Console.WriteLine(secondList[1].Age);    }}class Cat{    public string Name;    public string Breed;    public int Age;}
```

\
\

## Replies

### Reply by Anupam Mishra

Now I have found one solution of this problem that are fulfill my requirements, code are as follows:-\

```
using System;using System.Collections.Generic;using System.Linq;namespace List_Clone{   class Program    {        static void Main(string[] args)        {            List<Cat> firstList = new List<Cat>        {            new Cat {Name = "Indian", Breed = "Persian", Age = 15},            new Cat {Name = "Spenish", Breed = "Shy", Age = 17}        };            //    Copying whole list in second List            //   List<Cat> secondList = new List<Cat>(firstList);             List<Cat> secondList = new List<Cat>();             List<Cat> newList = firstList.Select(z => z.Clone() as Cat).ToList();             Console.WriteLine(newList[0].Age);             Console.WriteLine(newList[1].Age);             //
             Changing on second list             newList[0].Age++;             newList[1].Age++;             Console.WriteLine(newList[0].Age);             Console.WriteLine(newList[1].Age);             Console.WriteLine();             Console.WriteLine(firstList[0].Age);// For checking any changes or not?              Console.WriteLine( firstList[1].Age);// Not changes originally             Console.ReadKey();        }    }    class Cat : ICloneable    {        public string Name;        public string Breed;        public int Age;        public object Clone() // overload Clone() method        {            return this.MemberwiseClone();        }    }}
```

In above , i have implemented ICloneable interface for override Clone() method in Cat class. As above scenario, i have one list and we want to create another list with same content without affecting to first list content. I have created clone of firstList and copy content to secondList.Now, i have easily modify secondList.


---

Original Source: https://www.mindstick.com/forum/33852/how-to-copy-a-generic-list-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
