Implementing Stack in C# Last Updated : 30 Oct, 2018 Comments Improve Suggest changes Like Article Like Report Stack is a linear data structure. It follows LIFO(Last In First Out) pattern for Input/output. Following three basic operations are performed in the stack: Push: Adds an item in the stack. If the stack is full, then it is said to be a stack Overflow condition. Pop: Removes an item from the stack. The items are popped in the reversed order in which they are pushed. If the stack is empty, then it is said to be a stack Underflow condition. Peek : Return the topmost element of stack. Given below is the C# code to implement Stack using Arrays C using System; namespace StackImplementation { internal class Stack { static readonly int MAX = 1000; int top; int[] stack = new int[MAX]; bool IsEmpty() { return (top < 0); } public Stack() { top = -1; } internal bool Push(int data) { if (top >= MAX) { Console.WriteLine("Stack Overflow"); return false; } else { stack[++top] = data; return true; } } internal int Pop() { if (top < 0) { Console.WriteLine("Stack Underflow"); return 0; } else { int value = stack[top--]; return value; } } internal void Peek() { if (top < 0) { Console.WriteLine("Stack Underflow"); return; } else Console.WriteLine("The topmost element of Stack is : {0}", stack[top]); } internal void PrintStack() { if (top < 0) { Console.WriteLine("Stack Underflow"); return; } else { Console.WriteLine("Items in the Stack are :"); for (int i = top; i >= 0; i--) { Console.WriteLine(stack[i]); } } } } class Program { static void Main(string[] args) { Stack myStack = new Stack(); myStack.Push(10); myStack.Push(20); myStack.Push(30); myStack.Push(40); myStack.PrintStack(); myStack.Peek(); Console.WriteLine("Item popped from Stack : {0}", myStack.Pop()); myStack.PrintStack(); } } } Output: Items in the Stack are : 40 30 20 10 The topmost element of Stack is : 40 Item popped from Stack : 40 Items in the Stack are : 30 20 10 Comment More infoAdvertise with us Next Article Implementing Stack in C# A ankit sharma Improve Article Tags : Stack C# DSA CSharp-Generic-Stack CSharp-Generic-Namespace +1 More Practice Tags : Stack Similar Reads C# List Implementation In C#, a List is a generic collection used to store the elements or objects in the form of a list defined under System.Collection.Generic namespace. It provides the same functionality as ArrayList, the difference is a list is generic whereas ArrayList is a non-generic collection. It is dynamic means 7 min read C# | Explicit Interface Implementation An Interface is a collection of loosely bound items that have a common functionality or attributes. Interfaces contain method signatures, properties, events etc. Interfaces are used so that one class or struct can implement multiple behaviors. C# doesn't support the concept of Multiple Inheritance b 4 min read Collections in C# .math-table { border-collapse: collapse; width: 100%; } .math-table td { border: 1px solid #5fb962; text-align: left !important; padding: 8px; } .math-table th { border: 1px solid #5fb962; padding: 8px; } .math-table tr>th{ background-color: #c6ebd9; vertical-align: middle; } .math-table tr:nth-c 5 min read Delegates vs Interfaces in C# A Delegate is an object which refers to a method or you can say it is a reference type variable that can hold a reference to the methods. Delegates in C# are similar to the function pointer in C/C++. It provides a way which tells which method is to be called when an event is triggered. Example: CSha 3 min read C# | Inheritance in interfaces C# allows the user to inherit one interface into another interface. When a class implements the inherited interface then it must provide the implementation of all the members that are defined within the interface inheritance chain.Important Points: If a class implements an interface, then it is nece 3 min read Like