How do I clone a generic list in C#?Ask Questions

 Posted on 21 days ago

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?

Share On: facebook gplus twitter
profile
Asked by Soniya Disoza on 21 days ago Score: 14 points
Add Comment:

Comments

2 Answers

0 Corrected Answers
Aproved Answers
0
Profile
Answered by hardik chaudhary on 10/2/2019 11:24:03 AM Score: 542 points

If your elements are value types, then you can just do:

However, if they are reference types and you want a deep copy (assuming your elements properly implement ICloneable), you could do something like this:

List<ICloneable> oldList = new List<ICloneable>();

List<ICloneable> newList = new List<ICloneable>(oldList.Count);

oldList.ForEach((item) => {

newList.Add((ICloneable)item.Clone());

});

Obviously, replace ICloneable in the above generics and cast with whatever your element type is that implements ICloneable.

If your element type doesn't support ICloneable but does have a copy-constructor, you could do this instead:

List<YourType> oldList = new List<YourType>();

List<YourType> newList = new List<YourType>(oldList.Count);

oldList.ForEach((item)=> { newList.Add(new YourType(item)); });

Personally, I would avoid ICloneable because of the need to guarantee a deep copy of all members. Instead, I'd suggest the copy-constructor or a factory method like YourType.CopyFrom(YourType itemToCopy) that returns a new instance of YourType.

Any of these options could be wrapped by a method (extension or otherwise).

Comments

Add Comment:
Aproved Answers
1
Profile
Answered by Aidam Turner on 9/30/2019 2:18:01 PM Score: 50 points

You can use an extension method.

static class Extensions {

public static IList<T> Clone<T>(this IList<T> listToClone) where T: ICloneable

  {

      return listToClone.Select(item => (T)item.Clone()).ToList();

  }

}

Comments

Add Comment:

Post Your Answers

Existing Members

Sign in to your account
Email Address
Password
New Member?
Sign up and complete profile
Full Name
Email Address
I have read and agree to the Terms of Service and Privacy Policy
Please subscribe me to the StoodQ newsletters
Guideline to answer a question:

Useful tips to submit your answer
Please read below guidelines before you submit your answer for question.

  • Read and understand question for which you are submitting your answer.
  • Try to avoid grammatical and spell mistake while answering.
  • Do not post any irrelevant information in your answer.
  • Explain your answer with example or any reference link to help who posted question.
  • If you find irrelevant question, please report it to support. Click here to contact support.
  • You agree to the privacy policy and terms of use to submit any contents.

Note: StoodQ is online developers community which helps developer for their difficulty, lets help them with your value contribution.