Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Convert Stack to array in C#
Converting a Stack to an array in C# is accomplished using the ToArray() method. This method creates a new array containing all elements from the stack in the same order they would be popped (LIFO - Last In, First Out).
The Stack<T> class in C# is part of the System.Collections.Generic namespace and represents a collection that follows the Last In, First Out principle.
Syntax
Following is the syntax for converting a Stack to an array −
Stack<T> stack = new Stack<T>(); T[] array = stack.ToArray();
How It Works
The ToArray() method creates a shallow copy of the stack elements in an array. The order of elements in the resulting array matches the order in which they would be retrieved using Pop() operations, with the most recently added element at index 0.
Using ToArray() with String Stack
Example
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(){
Stack<string> stack = new Stack<string>();
stack.Push("AB");
stack.Push("CD");
stack.Push("FG");
stack.Push("KL");
Console.WriteLine("Stack elements:");
foreach(string i in stack){
Console.WriteLine(i);
}
string[] strArr = stack.ToArray();
Console.WriteLine("\nConverted Array:");
foreach(string i in strArr){
Console.WriteLine(i);
}
Console.WriteLine("\nArray Length: " + strArr.Length);
}
}
The output of the above code is −
Stack elements: KL FG CD AB Converted Array: KL FG CD AB Array Length: 4
Using ToArray() with Integer Stack
Example
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(){
Stack<int> stack = new Stack<int>();
stack.Push(250);
stack.Push(500);
stack.Push(750);
stack.Push(1000);
stack.Push(1200);
stack.Push(1500);
Console.WriteLine("Original Stack:");
foreach(int i in stack){
Console.WriteLine(i);
}
int[] intArr = stack.ToArray();
Console.WriteLine("\nArray after conversion:");
for(int i = 0; i < intArr.Length; i++){
Console.WriteLine($"Index [{i}]: {intArr[i]}");
}
}
}
The output of the above code is −
Original Stack: 1500 1200 1000 750 500 250 Array after conversion: Index [0]: 1500 Index [1]: 1200 Index [2]: 1000 Index [3]: 750 Index [4]: 500 Index [5]: 250
Key Points
-
The
ToArray()method does not modify the original stack − it creates a copy. -
Elements in the resulting array maintain the same order as they would be popped from the stack.
-
The method returns a new array with the same type as the stack's generic parameter.
-
If the stack is empty,
ToArray()returns an empty array, notnull.
Conclusion
The ToArray() method provides an efficient way to convert a Stack to an array in C#. The resulting array preserves the LIFO order of the stack, making it useful when you need random access to stack elements or want to pass stack data to methods expecting arrays.
