Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / August 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Sin Jeong-hun - 28 Jul 2006 18:23 GMT
List<List<T>> a=param;
List<List<T>> b=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,
List<List<T>> b=CreateClone(a);

Thanks.
Nicholas Paldino [.NET/C# MVP] - 28 Jul 2006 18:39 GMT
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.
List<List<T>> a = param;

// Make a copy.
List<List<T>> b = a.ConvertAll(delegate(T input) { return input; });

   Basically, you are passing the value through to a new list.

   Hope this helps.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> List<List<T>> a=param;
> List<List<T>> b=a;
[quoted text clipped - 5 lines]
>
> Thanks.
Sin Jeong-hun - 28 Jul 2006 19:20 GMT
Thank you all!

> Sin Jeong-hun,
>
[quoted text clipped - 26 lines]
> >
> > Thanks.
Jon Shemitz - 28 Jul 2006 19:56 GMT
> // Make a copy.
> List<List<T>> b = a.ConvertAll(delegate(T input) { return input; });

This won't compile - you need to write "ConvertAll<List<T>>". More
importantly, this only clones the 'outer list' - you get a new list of
the same inner List<T> elements. If you want a wholly independent copy
(so that changing one of the lists in the original tree doesn't affect
the copy) you need

List<List<T>> DeepCopy =
 List.ConvertAll<List<T>>(delegate(List<T> Branch)
 {
     return Branch.ConvertAll<T>(delegate(T Leaf){return Leaf;});
 });

Signature

.NET 2.0 for Delphi Programmers        www.midnightbeach.com/.net
Delphi skills make .NET easy to learn  In print, in stores.

kjetil.gloppen@gmail.com - 25 Aug 2006 09:51 GMT
> List<List<T>> DeepCopy =
>   List.ConvertAll<List<T>>(delegate(List<T> Branch)
>   {
>       return Branch.ConvertAll<T>(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:

List<List<T>> DeepCopy =
  List.ConvertAll<List<T>>(delegate(List<T> Branch)
  {
      return Branch.ConvertAll<T>(delegate(T Leaf){return
Leaf.Clone();});
  });

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.