How would I initialize these two lists so that modifying one doesn't modify the other?

Posted by Brandon on Stack Overflow See other posts from Stack Overflow or by Brandon
Published on 2010-12-23T18:25:40Z Indexed on 2010/12/29 17:54 UTC
Read the original article Hit count: 183

Filed under:
|
|
|

I'm aware of why this is happening, but is there any way to do this without having to implement ICloneable or a Copy() method? Preferably .net 2.0, but 3.5 is fine if it is necessary.

Essentially I'm trying to implement an undo method. In most cases I can just perform the reverse action in the Undo(), but for others that is not possible.

So I want to keep two lists. One for the list of items that I will be modifying, and one for the original, unmodified list of items. This way if I need to do an undo, I just delete the modified items and replace them with the originals. Most of the ways I've tried to assign the _originalItems variable doesn't work, so what would I need to do?

public MyClass(List<SelectedItems> selectedItems)
{
  _selectedItems = new List<SelectedItems>(selectedItems);
  _originalItems = ??
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about winforms