C# Stack

Stack Collections come under System.Collection.Generic namespace. It stores in the LIFO form i.e. Last In First Out. Add elements using the Push() method, whereas remove using Pop().

There are two types of Stacks in C#: Generic and Non-Generic. The Generic Stack class should be preferred over Non-Generic.

Create a Stack and Push elements

To create a Stack in C#, with string elements, use <string>. Set the type in angular braces as shown below:

Stack<string> stack = new Stack<string>();

Let us see an example of creating a stack and pushing/adding elements. To add elements i.e., insert the object at the top of the stack, use the Push() method:

using System;
using System.Collections.Generic;

namespace Studyopedia
{
  class Program
  {
    static void Main(string[] args)
    {
       // Create a stack
       Stack<string> stack = new Stack<string>();

       // Push/ Add 5 elements to the Stack
       stack.Push("Sachin");
       stack.Push("Amit");
       stack.Push("Virat");
       stack.Push("Rohit");
       stack.Push("Rahul");

       // Display the elements
       Console.WriteLine("Display the stack elements...\n");
       foreach (string s in stack)
       {
           Console.WriteLine(s);
       }  
    }
  }
}

Output

Display the stack elements...

Rahul
Rohit
Virat
Amit
Sachin

Pop an element

To retrieve and remove an element from the top of the stack, use the Pop() method. Let us see an example:

using System;
using System.Collections.Generic;

namespace Studyopedia
{
  class Program
  {
    static void Main(string[] args)
    {
       // Create a stack
       Stack<string> stack = new Stack<string>();

       // Push/ Add 5 elements to the Stack
       stack.Push("Sachin");
       stack.Push("Amit");
       stack.Push("Virat");
       stack.Push("Rohit");
       stack.Push("Rahul");

       // Display the stack elements
       Console.WriteLine("Display the stack elements...");
       foreach (string s in stack)
       {
           Console.WriteLine(s);
       }  
       
       // Pop en element i.e. the tomost element gets removed
       Console.WriteLine("\nPop an element = \n"+stack.Pop());
       
       // Display the updated stack elements
       Console.WriteLine("\nDisplay the updated stack elements...");
       foreach (string s in stack)
       {
           Console.WriteLine(s);
       }  
    }
  }
}

Output

Display the stack elements...
Rahul
Rohit
Virat
Amit
Sachin

Pop an element = 
Rahul

Display the updated stack elements...
Rohit
Virat
Amit
Sachin

Peek an element

To return the element at the top of the stack, without removing it, use the Peek() method. Let us see an example:

using System;
using System.Collections.Generic;

namespace Studyopedia
{
  class Program
  {
    static void Main(string[] args)
    {
       // Create a stack
       Stack<string> stack = new Stack<string>();

       // Push/ Add 5 elements to the Stack
       stack.Push("Sachin");
       stack.Push("Amit");
       stack.Push("Virat");
       stack.Push("Rohit");
       stack.Push("Rahul");

       // Display the stack elements
       Console.WriteLine("Display the stack elements...");
       foreach (string s in stack)
       {
           Console.WriteLine(s);
       }  
       
       // Peek an element i.e., return the topmost element
       Console.WriteLine("\nPeek an element = \n"+stack.Peek());    
    }
  }
}

Output

Display the stack elements...
Rahul
Rohit
Virat
Amit
Sachin

Peek an element = 
Rahul

Count the number of elements

The Count property is used in C# to count the number of elements. Let us see an example:

using System;
using System.Collections.Generic;

namespace Studyopedia
{
  class Program
  {
    static void Main(string[] args)
    {
       // Create a stack
       Stack<string> stack = new Stack<string>();

       // Push/ Add 5 elements to the Stack
       stack.Push("Sachin");
       stack.Push("Amit");
       stack.Push("Virat");
       stack.Push("Rohit");
       stack.Push("Rahul");

       // Display the stack elements
       Console.WriteLine("Display the stack elements...");
       foreach (string s in stack)
       {
           Console.WriteLine(s);
       }  
       
       // Count of stack elements
       Console.WriteLine("\nStack elements = \n"+stack.Count);    
    }
  }
}

Output

Display the stack elements...
Rahul
Rohit
Virat
Amit
Sachin

Stack elements = 
5

Remove all the elements

The Clear() method is used to remove all the stack elements. Let us see an example:

using System;
using System.Collections.Generic;

namespace Studyopedia
{
  class Program
  {
    static void Main(string[] args)
    {
       // Create a stack
       Stack<string> stack = new Stack<string>();

       // Push/ Add 5 elements to the Stack
       stack.Push("Sachin");
       stack.Push("Amit");
       stack.Push("Virat");
       stack.Push("Rohit");
       stack.Push("Rahul");

       // Display the stack elements
       Console.WriteLine("Display the stack elements...");
       foreach (string s in stack)
       {
           Console.WriteLine(s);
       }  
       
       // Count of stack elements
       Console.WriteLine("\nStack elements = \n"+stack.Count);    
       
       // Remove all the elements
       stack.Clear();
       
       // Count of stack elements after removing all the elements
       Console.WriteLine("\nStack elements (updated) = \n"+stack.Count);    
    }
  }
}

Output

Display the stack elements...
Rahul
Rohit
Virat
Amit
Sachin

Stack elements = 
5

Stack elements (updated) = 
0

Read More

C# Tuple
C# Queue
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment