- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Arithmetic Operators
- C# - Assignment Operators
- C# - Relational Operators
- C# - Logical Operators
- C# - Bitwise Operators
- C# - Miscellaneous Operators
- C# - Operators Precedence
- C# Conditional Statements
- C# - Decision Making
- C# - If
- C# - If Else
- C# - Nested If
- C# - Switch
- C# - Nested Switch
- C# - Switch Expressions
- C# Control Statements
- C# - Loops
- C# - For Loop
- C# - While Loop
- C# - Do While Loop
- C# - Nested Loops
- C# - Break
- C# - Continue
- C# - Foreach Loop
- C# - Goto Statement
- C# OOP & Data Handling
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - LINQ
- C# - IEnumerable vs IEnumerator
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Tasks and Parallel Programming
- C# - Multithreading
- C# - Extension Methods
C# Stack - Clear() Method
The C# stack Clear() method is used to empty the stack by removing all the elements from the stack.
When this method is executed, the count is set to zero, and references to other objects from the collection's elements are also released. This takes O(n) time complexity, where n is the count.
Syntax
Following is the syntax of the C# stack Clear() method −
public virtual void Clear ();
Parameters
This method does not accepts any parameters.
Return value
This method does not returns any value.
Example 1: Using the Clear() Method in Stack
Following is the basic example of the Clear() method to empty a stack −
using System;
using System.Collections;
class Example {
public static void Main() {
Stack myStack = new Stack();
// Inserting the elements into the Stack
myStack.Push(1);
myStack.Push(2);
myStack.Push(3);
myStack.Push(4);
myStack.Push(5);
Console.Write("Total number of elements in the Stack are: ");
Console.WriteLine(myStack.Count);
// Removing all elements from Stack
myStack.Clear();
Console.Write("Total number of elements in the Stack are: ");
Console.WriteLine(myStack.Count);
}
}
Output
Following is the output −
Total number of elements in the Stack are: 5 Total number of elements in the Stack are: 0
Example 2: Clear All String From Stack
Let's see another example of the Clear() method to clear all the strings from the stack −
using System;
using System.Collections;
class Example {
public static void Main() {
Stack myStack = new Stack();
// Inserting the elements into the Stack
myStack.Push("Hello");
myStack.Push("tutorialspoint");
myStack.Push("India");
myStack.Push("Good to know");
Console.Write("Total number of elements in the Stack are: ");
Console.WriteLine(myStack.Count);
// Removing all elements from Stack
myStack.Clear();
Console.Write("Total number of elements in the Stack are: ");
Console.WriteLine(myStack.Count);
}
}
Output
Following is the output −
Total number of elements in the Stack are: 4 Total number of elements in the Stack are: 0
Example 3: Clear a Person Object from Stack
The below example creates a person object. Here, we use the Clear() method to clear all the details of people from the person object −
using System;
using System.Collections.Generic;
class Person {
public string Name { get; set; }
public int Age { get; set; }
public Person(string name, int age) {
Name = name;
Age = age;
}
public override string ToString() {
return $"Name: {Name}, Age: {Age}";
}
}
class Example {
static void Main() {
// Create a stack of Person objects
Stack<Person> people = new Stack<Person>();
// Push Person objects onto the stack
people.Push(new Person("Aman", 25));
people.Push(new Person("Gupta", 24));
people.Push(new Person("Vivek", 26));
Console.WriteLine("People stack before Clear:");
foreach (var person in people) {
Console.WriteLine(person);
}
// Clear the stack
people.Clear();
Console.WriteLine("\nPeople stack after Clear:");
Console.WriteLine("Is Empty? " + (people.Count == 0 ? "Yes" : "No"));
}
}
Output
Following is the output −
People stack before Clear: Name: Vivek, Age: 26 Name: Gupta, Age: 24 Name: Aman, Age: 25 People stack after Clear: Is Empty? Yes
csharp_stack.htm
Advertisements