top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to Clone a Generic List in C#?

+1 vote
382 views
How to Clone a Generic List in C#?
posted May 9, 2016 by Latha

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes
using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace AbundantcodeConsoleApp

{

 internal class Program

 {

  private static void Main(string[] args)

  {

   List<string> Names = new List<string>();

   Names.Add("Abundantcode.com");

   Names.Add("Tutorial Website");

   List<string> clonedList = Names.CloneList().ToList();

   foreach (var item in clonedList)

    Console.WriteLine(item);

   Console.ReadKey();

  }

}

internal static class Extensions

{

 public static IList<T> CloneList<T>(this IList<T> list) where T : ICloneable

 {

  return list.Select(item => (T)item.Clone()).ToList();

 }

}

}
answer May 9, 2016 by Shivaranjini
...