Home > DeveloperSection > Forums > How to copy a generic list in c#?
Anupam Mishra

Total Post:135

Points:949
Posted on    January-11-2016 9:52 PM

 C# C#  .NET 
Ratings:


 1 Reply(s)
 513  View(s)
Rate this:
I have two generic lists and I want to copy the contents of the first one to the second and then modify either list without affecting the other one and google hasn't been of much help since none of the proposed 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 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;
}





Anupam Mishra

Total Post:135

Points:949
Posted on    January-12-2016 2:47 AM

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.

Modified On Jan-12-2016 03:21:32 AM

Don't want to miss updates? Please click the below button!

Follow MindStick