
- 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
Stack with Examples in C#
The Stack class in C# represents a simple last-in-first-out (LIFO) non-generic collection of objects.
Following are the properties of the Stack class −
Sr.No | Property & Description |
---|---|
1 | Count Gets the number of elements contained in the Stack. |
2 | IsSynchronized Gets a value indicating whether access to the Stack is synchronized (thread safe). |
3 | SyncRoot Gets an object that can be used to synchronize access to the Stack. |
Following are some of the methods of the Stack class −
Sr.No | Property & Description |
---|---|
1 | Clear() Removes all objects from the Stack. |
2 | Clone() Creates a shallow copy of the Stack. |
3 | Contains(Object) whether an element is in the Stack. |
4 | CopyTo(Array, Int32) Copies the Stack to an existing one-dimensional Array, starting at the specified array index. |
5 | Equals(Object) Determines whether the specified object is equal to the current object. |
6 | GetEnumerator() Returns an IEnumerator for the Stack. |
7 | GetHashCode() Serves as the default hash function. (Inherited from Object) |
8 | GetType() Gets the Type of the current instance. |
9 | Peek() Returns the object at the top of the Stack without removing it. |
10 | Pop() Removes and returns the object at the top of the Stack |
11 | Push(Object) Inserts an object at the top of the Stack. |
Example
Let us now see some of the examples −
To get object at the top of the Stack, the code is as follows −
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"); stack.Push("I"); stack.Push("J"); Console.WriteLine("Count of elements = "+stack.Count); Console.WriteLine("Element at the top of stack = " + stack.Peek()); } }
Output
This will produce the following output −
Count of elements = 10 Element at the top of stack = J Count of elements = 10
To check if a Stack has an elements, use the C# Contains() method. Following is the code −
Example
using System; using System.Collections.Generic; public class Demo { public static void Main() { Stack<int> stack = new Stack<int>(); stack.Push(100); stack.Push(150); stack.Push(175); stack.Push(200); stack.Push(225); stack.Push(250); stack.Push(300); stack.Push(400); stack.Push(450); stack.Push(500); Console.WriteLine("Elements in the Stack:"); foreach(var val in stack) { Console.WriteLine(val); } Console.WriteLine("Count of elements in the Stack = "+stack.Count); Console.WriteLine("Does Stack has the element 400?= "+stack.Contains(400)); } }
Output
This will produce the following output −
Elements in the Stack: 500 450 400 300 250 225 200 175 150 100 Count of elements in the Stack = 10 Does Stack has the element40400?= False
- Related Articles
- Explain Stack in Python with examples
- Vertically stack pills with Bootstrap
- Vertically stack tabs with Bootstrap
- Design a Stack With Increment Operation in C++
- Stack multiple progress bars with Bootstrap
- Timeit in Python with Examples?
- negative_binomial_distribution in C++ with Examples
- Popovers in Bootstrap with examples
- Queue with Examples in C#?
- SortedList with Examples in C#?
- ratio_not_equal() in C++ with examples
- Decimal to Multiple-Bases Conversion with Stack
- Stack and the stack pointer in 8085 Microprocessor
- stack empty() and stack size() in C++ STL
- Regular Expression in Python with Examples?

Advertisements