By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,261 Members | 2,662 Online
Bytes IT Community
+ Ask a Question
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
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.

Jul 28 '06 #1
Share this Question
Share on Google+
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.
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Sin Jeong-hun" <ty*******@gmail.comwrote in message
news:11**********************@m79g2000cwm.googlegr oups.com...
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.

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.
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Sin Jeong-hun" <ty*******@gmail.comwrote in message
news:11**********************@m79g2000cwm.googlegr oups.com...
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.
Jul 28 '06 #3

P: n/a
"Nicholas Paldino [.NET/C# MVP]" wrote:
// 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<Telements. 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<TBranch)
{
return Branch.ConvertAll<T>(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
List<List<T>DeepCopy =
List.ConvertAll<List<T>>(delegate(List<TBranch)
{
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<TBranch)
{
return Branch.ConvertAll<T>(delegate(T Leaf){return
Leaf.Clone();});
});

Aug 25 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.