Posts

Showing posts from July, 2015

Clone a Generic List in C#

I have a generic list of objects in C#, and wish to clone the list. The items within the list are cloneable, but there doesn't seem to be an option to do list.Clone()
Is there an easy way around this?
You can use an extension method.
staticclassExtensions{publicstaticIList<T>Clone<T>(thisIList<T> listToClone)where T:ICloneable{return listToClone.Select(item =>(T)item.Clone()).ToList();}}Source on StackOverFlow