Stack.Clear Method in C#
This method(comes under System.Collections namespace) is used to remove all the objects from the Stack. This method will set the Count of Stack to zero, and references to other objects from elements of the collection are also removed. This method is an O(n) operation, where n is Count.
Syntax:
public virtual void Clear ();
Below programs illustrate the use of above-discussed method:
Example 1:
// C# code to illustrate the // Stack.Clear Method using System; using System.Collections; class GFG { // Driver code public static void Main() { // Creating a Stack Stack myStack = new Stack(); // Inserting the elements into the Stack myStack.Push("1st Element"); myStack.Push("2nd Element"); myStack.Push("3rd Element"); myStack.Push("4th Element"); myStack.Push("5th Element"); myStack.Push("6th Element"); // Displaying the count of elements // contained in the Stack before // removing all the elements Console.Write("Total number of elements"+ " in the Stack are : "); Console.WriteLine(myStack.Count); // Removing all elements from Stack myStack.Clear(); // Displaying the count of elements // contained in the Stack after // removing all the elements Console.Write("Total number of elements"+ " in the Stack are : "); Console.WriteLine(myStack.Count); } } |
chevron_right
filter_none
Output:
Total number of elements in the Stack are : 6 Total number of elements in the Stack are : 0
Example 2:
// C# code to illustrate the // Stack.Clear Method using System; using System.Collections; class GFG { // Driver code public static void Main() { // Creating a Stack Stack myStack = new Stack(); // Inserting the elements into the Stack myStack.Push(3); myStack.Push(5); myStack.Push(7); myStack.Push(9); myStack.Push(11); // Displaying the count of elements // contained in the Stack before // removing all the elements Console.Write("Total number of elements "+ "in the Stack are : "); Console.WriteLine(myStack.Count); // Removing all elements from Stack myStack.Clear(); // Displaying the count of elements // contained in the Stack after // removing all the elements Console.Write("Total number of elements"+ " in the Stack are : "); Console.WriteLine(myStack.Count); } } |
chevron_right
filter_none
Output:
Total number of elements in the Stack are : 5 Total number of elements in the Stack are : 0
Reference:
Recommended Posts:
- Difference between Method Overriding and Method Hiding in C#
- Queue.Contains() Method in C#
- C# | Math.Tan() Method
- C# | Trim() Method
- C# | SortedDictionary.Add() Method
- C# | ToCharArray() Method
- C# | Method Parameters
- C# | String.Contains() Method
- C# | IndexOfAny() Method
- C# | Math.Sin() Method
- Anonymous Method in C#
- C# | Uri.IsHexEncoding() Method
- C# | IsNullOrWhiteSpace() Method
- C# | Math.Cos() Method
- C# | Insert() Method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



