C# Stack - ToArray() Method



The C# stack ToArray() method is used to copy the stack into a new array. Elements are copied to the array in a last-in-first-out (LIFO) order, similar to the order of elements returned by the pop method. This method takes O(n) operation, where n is the number of elements or count.

Syntax

Following is the syntax of the C# stack ToArray() method −

public virtual object?[] ToArray ();

Parameters

This method does not accepts any parameters.

Return value

This method returns a new array containing copies of the elements of the Stack.

Example 1: Copying The Stack onto An Array

Following is the basic example to demonstrate the use of ToArray() method −

using System;
using System.Collections.Generic;
class Program {
   static void Main() {
      Stack<string> stack = new Stack<string>();
   
      stack.Push("First");
      stack.Push("Second");
      stack.Push("Third");
      stack.Push("Fourth");
   
      // Convert stack to array
      string[] array = stack.ToArray();
   
      // Display the array elements
      Console.WriteLine("Array Elements:");
      foreach (var item in array) {
         Console.WriteLine(item);
      }
   }
}

Output

Following is the output −

Array Elements:
Fourth
Third
Second
First

Example 2: Processing Stack Data

This example demonstrates how to convert a stack to an array and then perform operations on the array without modifying the original stack −

using System;
using System.Collections.Generic;
class Program {
   static void Main() {
      Stack<int> numbers = new Stack<int>();
   
      numbers.Push(10);
      numbers.Push(20);
      numbers.Push(30);
      numbers.Push(40);
      numbers.Push(50);
   
      // Convert stack to array
      int[] numArray = numbers.ToArray();
   
      // Display the array elements
      Console.WriteLine("Array Elements (Converted from Stack):");
      foreach (var num in numArray) {
         Console.Write(num + " ");
      }
   
      // Finding the sum of elements in the array
      int sum = 0;
      foreach (var num in numArray) {
         sum += num;
      }
      Console.WriteLine("\n\nSum of Array Elements: " + sum);
   }
}

Output

Following is the output −

Array Elements (Converted from Stack):
50 40 30 20 10 

Sum of Array Elements: 150

Example 3: Reversing a Sentence

This example demonstrates how to use a stack to reverse the words in a sentence and then convert the stack into an array −

using System;
using System.Collections.Generic;

class Example3 {
   static void Main() {
      string sentence = "C# Stack ToArray Example";
      Stack<string> wordsStack = new Stack<string>();

      // Split the sentence into words and push them onto the stack
      foreach (string word in sentence.Split(' '))
      {
         wordsStack.Push(word);
      }

      // Convert stack to array
      string[] reversedWords = wordsStack.ToArray();

      // Display reversed sentence
      Console.WriteLine("Reversed Sentence: " + string.Join(" ", reversedWords));
   }
}

Output

Following is the output −

Reversed Sentence: Example ToArray Stack C#
csharp_stack.htm
Advertisements