0

i use a c# win application. i would to make a copy of my list not for refereance but by value. i would my first list remains always original and if i make a change in new list, this change doesn't appear in original list. i have tried this code but doesn't wotk beacuse copy to reference and not to value

_TracList = GetMyData();
List<FileVers> wTemp = new List<FileVers>(_TracList);

i would modify wTemp and take _TracList with original value; how can i make a copy by value of my List?

user1581365
  • 21
  • 1
  • 8

1 Answers1

0

Your code does create a value copy: If you remove an element from wTemp, it will still be available in _TracList.

However, your code does not make a value copy of each element, so if you modify an entry in wTemp, it will also be modified in _TracList. C# cannot make a deep copy, since it has no idea how to make a value copy of your custom FileVers class.

If you want a copy of each element, you will have to create it manually:

var wTemp = _TracList.Select(x => MakeCopyOf(x)).ToList();

MakeCopyOf is a method that you must supply, which takes a FileVers instance and returns a copy of it.

Heinzi
  • 167,459
  • 57
  • 363
  • 519