0

for an undo-redo system i need to call a function that does a deepcopy of a list everytime the list is changed in some way (add, remove, insert, modify element at an index).

I thought about making the list private and writing a method for each of the operations. However this adds a lot of bulky code. Also it is not very practical and looks not so nice for example list[i] = 10 becomes modify(list, i, 10). The modify method looks like this:

public void Modify(List<int> list, int index, int value) {
    Logger.DeepCopy(list);
    list[i] = value;
}

Is there a way to keep the methods of the list as they are?

Mycom
  • 39
  • 5
  • Look into properties, `get` and `set` in particular would help you. – Ally Apr 06 '18 at 11:44
  • 3
    Did you search for "notifyable list in c#'"? Leads me to [`ObservableCollection`](https://msdn.microsoft.com/library/ms668604(v=vs.110).aspx). – MakePeaceGreatAgain Apr 06 '18 at 11:45
  • i also thought about properties. But the setter is only triggered if the list ist set, not if it is modified. And the getter is not ideal since i dont want to Log the list if it is only read – Mycom Apr 06 '18 at 11:47
  • @HimBromBeere i guess that's it. thank you for the quick answer! – Mycom Apr 06 '18 at 11:48

0 Answers0