429,261 Members | 2,662 Online
Need help? Post your question and get tips & solutions from a community of 429,261 IT Pros & Developers. It's quick & easy.

# Easy way to copy a List<List<T>>

 P: n/a Lista=param; Listb=a; If I change b, then a is get changed. I want another copy of a, that is completely independent of a. I used double-nested for loop to copy each element manually. Is there any more efficent way to do that? Something like, Listb=CreateClone(a); Thanks. Jul 28 '06 #1
4 Replies

 P: n/a Sin Jeong-hun, The easiest way to do this would be to use the ConvertAll method on the instance itself. You can always convert the list to itself. =) So, you can do: // This assumes T is a valid type. Lista = param; // Make a copy. Listb = a.ConvertAll(delegate(T input) { return input; }); Basically, you are passing the value through to a new list. Hope this helps. -- - Nicholas Paldino [.NET/C# MVP] - mv*@spam.guard.caspershouse.com "Sin Jeong-hun" a=param; Listb=a; If I change b, then a is get changed. I want another copy of a, that is completely independent of a. I used double-nested for loop to copy each element manually. Is there any more efficent way to do that? Something like, Listb=CreateClone(a); Thanks. Jul 28 '06 #2

 P: n/a Thank you all! Nicholas Paldino [.NET/C# MVP] wrote: Sin Jeong-hun, The easiest way to do this would be to use the ConvertAll method on the instance itself. You can always convert the list to itself. =) So, you can do: // This assumes T is a valid type. Lista = param; // Make a copy. Listb = a.ConvertAll(delegate(T input) { return input; }); Basically, you are passing the value through to a new list. Hope this helps. -- - Nicholas Paldino [.NET/C# MVP] - mv*@spam.guard.caspershouse.com "Sin Jeong-hun" a=param; Listb=a; If I change b, then a is get changed. I want another copy of a, that is completely independent of a. I used double-nested for loop to copy each element manually. Is there any more efficent way to do that? Something like, Listb=CreateClone(a); Thanks. Jul 28 '06 #3

 P: n/a "Nicholas Paldino [.NET/C# MVP]" wrote: // Make a copy. Listb = a.ConvertAll(delegate(T input) { return input; }); This won't compile - you need to write "ConvertAll>". More importantly, this only clones the 'outer list' - you get a new list of the same inner ListDeepCopy = List.ConvertAll>(delegate(List(delegate(T Leaf){return Leaf;}); }); -- ..NET 2.0 for Delphi Programmers www.midnightbeach.com/.net Delphi skills make .NET easy to learn In print, in stores. Jul 28 '06 #4

 P: n/a ListDeepCopy = List.ConvertAll>(delegate(List(delegate(T Leaf){return Leaf;}); }); If T is a reference type, this method will still not be "deep" enough. The leafs will just be refering to the same objects. To fix this I guess you could make sure T inmplementes ICloneable and let Clone() return a deap copy of T. Then the code should be: ListDeepCopy = List.ConvertAll>(delegate(List(delegate(T Leaf){return Leaf.Clone();}); }); Aug 25 '06 #5

### This discussion thread is closed

Replies have been disabled for this discussion.