When to use the Stack collection in C#?

后端 未结 12 2291
滥情空心
滥情空心 2021-02-14 20:44

I do understand how Stack() and Stack works, but I really can\'t see any scenarios where an array, List or IEnumer

12条回答
  •  一向
    一向 (楼主)
    2021-02-14 21:00

    Heres one way I've used Stack:

    I have a wizard like page where there are 5 user controls that need to be dispalyed and processed in a specific sequence, and the user should be able at any point to return to the last page in order.

    I use a stack based state machine to track the users progress through the wizard.

    Each time they move forward, I push the state of the state machine into a session stored stack, and if they request to go back, I pop off the top value and set the machine to the new top stack value. (Which in turn, displays and loads the proper control)

    One more:

    I had to build an install application for some very custom server applications. What I ended up doing was breaking each step of the install into its own generic component, ie, a component to copy a file, a component to write a registry value, ect. Each compontent had to methods: Do the install action, Undo the install action.

    Each step of the install, the component is pushed into a stack.

    Now, at any time I have an error, we just pop each component back off the stack, and run the undo action giving us fine grained install transactions.

    Stack's are your friend!

提交回复
热议问题