Create a Stack from a collection in C#

To create a Stack from a collection in C#, you can use the Stack<T> constructor that accepts an IEnumerable<T> parameter. This allows you to initialize a stack with elements from arrays, lists, or other collections.

Syntax

Following is the syntax for creating a Stack from a collection −

Stack<T> stack = new Stack<T>(IEnumerable<T> collection);

The elements are pushed in the reverse order of the collection enumeration.

Creating Stack from Array

The following example shows how to create a Stack from an existing array −

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      int[] numbers = {100, 200, 300, 400, 500};
      
      Console.WriteLine("Original Array:");
      foreach(int val in numbers) {
         Console.WriteLine(val);
      }
      
      Stack<int> stack = new Stack<int>(numbers);
      
      Console.WriteLine("\nStack elements (LIFO order):");
      foreach(int val in stack) {
         Console.WriteLine(val);
      }
   }
}

The output of the above code is −

Original Array:
100
200
300
400
500

Stack elements (LIFO order):
500
400
300
200
100

Creating Stack from Another Stack

You can also create a Stack from another Stack using the ToArray() method −

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      Stack<string> originalStack = new Stack<string>();
      originalStack.Push("First");
      originalStack.Push("Second");
      originalStack.Push("Third");
      originalStack.Push("Fourth");
      
      Console.WriteLine("Original Stack:");
      foreach(string val in originalStack) {
         Console.WriteLine(val);
      }
      
      Stack<string> newStack = new Stack<string>(originalStack.ToArray());
      
      Console.WriteLine("\nNew Stack from ToArray():");
      foreach(string val in newStack) {
         Console.WriteLine(val);
      }
   }
}

The output of the above code is −

Original Stack:
Fourth
Third
Second
First

New Stack from ToArray():
First
Second
Third
Fourth

Creating Stack from List

Here's how to create a Stack from a List collection −

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      List<int> numbers = new List<int> {10, 20, 30, 40, 50};
      
      Console.WriteLine("List elements:");
      foreach(int val in numbers) {
         Console.WriteLine(val);
      }
      
      Stack<int> stack = new Stack<int>(numbers);
      
      Console.WriteLine("\nStack from List:");
      foreach(int val in stack) {
         Console.WriteLine(val);
      }
      
      Console.WriteLine("\nPopping elements:");
      while(stack.Count > 0) {
         Console.WriteLine("Popped: " + stack.Pop());
      }
   }
}

The output of the above code is −

List elements:
10
20
30
40
50

Stack from List:
50
40
30
20
10

Popping elements:
Popped: 50
Popped: 40
Popped: 30
Popped: 20
Popped: 10

How It Works

When creating a Stack from a collection, the constructor iterates through the collection and pushes each element onto the stack. This results in the last element of the collection becoming the top of the stack (LIFO - Last In, First Out principle).

Collection to Stack Conversion Array [1, 2, 3, 4] 1 2 3 4 Stack (LIFO) 4 ? Top 3 2 1 Last array element becomes stack top

Conclusion

Creating a Stack from a collection in C# is straightforward using the Stack<T>(IEnumerable<T>) constructor. The key point to remember is that the collection elements are pushed in reverse order, making the last collection element the top of the stack, maintaining the LIFO behavior.

Updated on: 2026-03-17T07:04:36+05:30

175 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements