Below is a sample code snippet that demonstrates how to clone a generic list in C#.
How to Clone a Generic List in C#?
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(); } } }
Leave a Reply