- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Insert an object at the top of the Stack in C#
To insert an object at the top of the Stack, the code is as follows −
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); 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)); stack.Push(300); stack.Push(400); stack.Push(450); stack.Push(500); Console.WriteLine("Elements in the Stack... (UPDATED)"); foreach(var val in stack) { Console.WriteLine(val); } Console.WriteLine("Count of elements in the Stack (UPDATED) = "+stack.Count); } }
Output
This will produce the following output −
Elements in the Stack: 250 225 200 175 150 100 Count of elements in the Stack = 6 Does Stack has the element 400?= False Elements in the Stack... (UPDATED) 500 450 400 300 250 225 200 175 150 100 Count of elements in the Stack (UPDATED) = 10
Example
Let us see another 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.Push("M"); Console.WriteLine("Elements in the Stack... (UPDATED)"); foreach(var val in stack) { Console.WriteLine(val); } Console.WriteLine("Count of elements in the Stack (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 Elements in the Stack... (UPDATED) M H G F E D C B A Count of elements in the Stack (UPDATED) = 9
- Related Articles
- Get object at the top of the Stack in C#
- How to move an object to the top of the stack of drawn objects in IText using FabricJS?
- stack top() in C++ STL
- FabricJS – How to move a Line object to the top of the stack of drawn objects?
- Insert an element into the ArrayList at the specified index in C#
- How to insert an object in an ArrayList at a specific position in java?
- How to bring an activity to the foreground i.e. top of stack using Kotlin?
- How to insert an object in a list at a given position in Python?
- Insert at the specified index in StringCollection in C#
- Copy the Stack to an Array in C#
- Insert an element at second position in a C# List
- Insert an element into Collection at specified index in C#
- How to bring an activity to foreground i.e. top of stack in Android?
- The temperature at the foot of a mountain is $+$ 8°C. It fell down by 10°C at the top of the mountain. The temperature recorded at the top is?
- Get the object at the beginning of the Queue – Peek Operation in C#

Advertisements