Search code examples
c#wpfstackundo-redo

Wants next Stack<T> from the list of Actions


I am making a UndoRedo Functionlity for my application. Now on function DoAction I have the list of Action taken in Stack<UndoRedoAction> Now I want to get the Last Action Taken Which automatically will be First in the List To take out the First in the List I have used actionList.Peek(); Now the Situation arise is that the next time I wanted to take the second one from the list. I am not sure how to do that

private void DoAction(Stack<UndoRedoAction> actionList, Stack<UndoRedoAction> storeList, Action<UndoRedoAction> action)
{
    if (actionList.Count > 0)
    {
        UndoRedoAction urAction = actionList.Peek();
        action(urAction);
        storeList.Push(urAction);
    }
}

Solution

  • You need to use Stack<T>.Pop instead of Peek. Pop removes the last added item in the stack and returns it while Peek returns the last added item without removing it from the stack.