Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy List in C#

I want to copy a List of Objects, but i keep getting references between the objects.

List<MyClass> copy = original;

When i change the value in the copy List, the original List ends up modified also. Is there a way to work around it?

like image 326
ricojohari Avatar asked Mar 11 '13 03:03

ricojohari


1 Answers

You can do this:

List<MyClass> copy = original.ToList();

This would make an element-by-element copy of the list, rather than copying the reference to the list itself.

like image 191
Sergey Kalinichenko Avatar answered Oct 01 '22 06:10

Sergey Kalinichenko