5
Answers

How to copy one element of a Generic Collection?

Javier

Javier

13y
3.6k
1

I need to copy one element of a generic collection and add it to the list. Something similar to this:
 

private List<CalculationResult> cantileverResults = new List<CalculationResult>();
cantileverResults.Add(cantileverResults[previousIndex]);
The problem with this solution is that when I modify the new element, the previousIndex element changes as well. I believe this is because they are reference-type, not value-type. How can I just copy (clone?) the information from one element to another without affecting each other any further?

Answers (5)