Stack.Clear Method in C# - GeeksforGeeks

04 Feb, 2019
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 Methodusing 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);    }}
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 Methodusing 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);    }}
Total number of elements in the Stack are : 5
Total number of elements in the Stack are : 0

Reference:
https://docs.microsoft.com/en-us/dotnet/api/system.collections.stack.clear?view=netframework-4.7.2
CSharp-Collections-Namespace
CSharp-Collections-Stack
CSharp-method
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between Abstract Class and Interface in C#
String.Split() Method in C# with Examples
C# | How to check whether a List contains a specified element
C# | IsNullOrEmpty() Method
C# Dictionary with examples
C# | Delegates
C# | Arrays of Strings
C# | Method Overriding
C# | Abstract Classes
Difference between Ref and Out keywords in C#