Stack.Clear Method in C#

In C# language, the Stack.clear() is a method that is useful for removing all the elements from the Stack at a time. It plays a vital role in managing the contents of a stack in C#. This method is used to reset the Stack to an empty state.

Syntax:

It has the following syntax:

The Clear () method will remove all the elements from the Stack named stackName in the syntax. The Stack.Nameclear() method will give a better performance while clearing the entire Stack at once rather than popping the elements one by one. This method is thread safety in multi-threading applications. This method is used in documentation and increases the code readability.

Example:

Let us take a program to demonstrate the Stack.Clear() method in C#.

Output:

Stack.Clear Method in C#

Explanation:

In this C# program, a stack named Stack is created. After that, the Push () method pushes three integers into the Stack. Next, all the elements in the Stack are printed using the custom PrintStack() method. The Clear () method removes all the integers from the Stack. After that, we try to print the integers from the Stack again. Finally, no integer will be printed.

Uses of Stack.Clear() Method:

There are some situations where the Stack.Clear() method are used.

Memory Management:

When dealing with large datasets or temporary data structures, clearing the Stack ensures that unnecessary memory is promptly released, preventing memory leaks and enhancing overall application performance.

Resetting State:

In implementing the undo/redo functionality, invoke Stack.Clear can reset the Stack to its initial state, providing a clean stack for subsequent actions.

Error Handling:

Stack.Clear method can revert to a stable state when an unexpected error occurs where a stack may be employed to track the execution context.

Example:

Let us take an example to illustrate the Stack.Clear() method in simple undo and redo functionality.

Output:

Stack.Clear Method in C#

Explanation:

In this example, the C# program explains the usage of the Stack.Clear() method. This program contains variables, undoRedoStack, which is used to track the undo and redo actions in the applications, and currentState represents the current state of the application. The methods present in the program are PerformAction(), which is used for pushing it onto the undo/redo Stack; ClearUndoRedoStack(), which is a method representing the User's decision to undo all the actions by clearing the undo/redo Stack, DisplayClearedStack() is a method to display the contents of the undo/redo Stack after it has been cleared.

Control Flow of the Program:

The undoRedoStack is initialized as a new Stack<string>. The currentState is set to "Initial State". The current state ("Initial State") is displayed. The PerformAction is called twice, simulating user actions ("Action 1" and "Action 2"). The actions are pushed onto the undo/redo Stack. The current state is displayed again, which remains "Initial State" as the current state variable is not updated during the simulation. ClearUndoRedoStack is called clearing the undo/redo Stack using the Stack.Clear method. The DisplayClearedStack is called, showing that the undo/redo stack is now empty.

Example:

Let us take a C# program to illustrate the Stack.Clear() method in a recursive algorithm.

Output:

Stack.Clear Method in C#

Explanation:

This program uses a recursive algorithm using a stack to track intermediate results. The RecursiveAlgorithm method pushes integers onto the Stack in a recursive manner, starting from 5 and decreasing until reaching 1. After the recursion, the program prints the contents of the Stack, showcasing the intermediate results accumulated during the recursive calls. After that, the Stack is cleared using Stack.Clear, the program displays an empty stack, demonstrating the clearing process after the recursive algorithm execution.






Latest Courses