I'm using a list of Actions to store an undo history for an object. Let's say I have a property of my object called myChildObject and it's being mutated by a method call, so I want to store the undo action where I would mutate it back to it's current value:
public class Class1
{
public Class1()
{
}
private readonly List<Action> m_undoActions = new List<Action>();
private SomeObject myChildObject { get; set; }
public void ChangeState()
{
m_undoActions.Add(() => myChildObject.UndoChangeState());
myChildObject.ChangeState();
}
}
Looking at the lambda expression, is the reference to myChildObject (the object) passed or is the reference to 'this' passed. Do I need to use 'this' to preface it? Do I need to make a copy of the 'this' reference to a local variable first?
Thanks for helping me understand this closure stuff.