
- C# Basic Tutorial
- 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# - Decision Making
- C# - Loops
- 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# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
Remove all objects from the Stack in C#
To remove all objects from the Stack, the code is as follows −
Example
using System; using System.Collections.Generic; public class Demo { public static void Main(){ Stack<string> stack = new Stack<string>(); stack.Push("A"); stack.Push("B"); stack.Push("C"); stack.Push("D"); stack.Push("E"); stack.Push("F"); stack.Push("G"); stack.Push("H"); Console.WriteLine("Count of elements = "+stack.Count); Console.WriteLine("Elements in Stack..."); foreach (string res in stack){ Console.WriteLine(res); } stack.Clear(); Console.Write("Count of elements (updated) = "+stack.Count); } }
Output
This will produce the following output −
Count of elements = 8 Elements in Stack... H G F E D C B A Count of elements (updated) = 0
Example
Let us see another example −
using System; using System.Collections.Generic; public class Demo { public static void Main(){ Stack<int> stack = new Stack<int>(); stack.Push(10); stack.Push(20); stack.Push(30); stack.Push(40); stack.Push(50); stack.Push(60); stack.Push(70); stack.Push(80); stack.Push(90); stack.Push(100); Console.WriteLine("Count of elements = "+stack.Count); Console.WriteLine("Elements in Stack..."); foreach (int res in stack){ Console.WriteLine(res); } stack.Clear(); Console.Write("Count of elements (updated) = "+stack.Count); } }
Output
This will produce the following output −
Count of elements = 10 Elements in Stack... 100 90 80 70 60 50 40 30 20 10 Count of elements (updated) = 0
- Related Articles
- Remove all objects from the Queue in C#
- How to remove all blank objects from an Object in JavaScript?
- Remove an element from a Stack in Java
- How to remove a parameter from all Request Objects at Controller Level in Laravel?
- Remove all elements from the ArrayList in Java
- Remove all elements from the ArrayList in C#
- Remove all elements from the SortedSet in C#
- Remove all entries from the ListDictionary in C#
- Remove all elements from the Collection in C#
- Remove all elements from the Hashtable in C#
- How to remove all objects except one or few in R?
- Remove all the strings from the StringCollection in C#
- Remove duplicates from a array of objects JavaScript
- Remove all values from TreeMap in Java
- Remove all elements from TreeSet in Java

Advertisements