Stack.CopyTo() Method in C#

The Stack.CopyTo() method in C# is used to copy all elements of a Stack to an existing one-dimensional Array, starting at a specified array index. This method is useful when you need to transfer stack data into an array for further processing or storage.

Syntax

Following is the syntax of the Stack.CopyTo() method −

public virtual void CopyTo(Array arr, int index);

Parameters

  • arr − The one-dimensional Array that is the destination of the elements copied from Stack. The Array must have zero-based indexing.

  • index − The zero-based index in the destination array at which copying begins.

Return Value

This method does not return a value. It copies the Stack elements directly into the specified array.

How It Works

The CopyTo() method copies elements from the Stack to the array in LIFO (Last-In-First-Out) order, which means the most recently pushed element appears first in the destination array. The destination array must be large enough to accommodate all stack elements starting from the specified index.

Stack.CopyTo() Process Original Stack 30 (Top) 20 10 (Bottom) CopyTo() Destination Array 30 [0] 20 [1] 10 [2] null [3] Elements copied in LIFO order (Most recent element first)

Using Stack.CopyTo() with Integer Elements

Example

using System;
using System.Collections;

public class Demo {
   public static void Main() {
      Stack stack = new Stack();
      stack.Push(100);
      stack.Push(200);
      stack.Push(300);
      stack.Push(400);
      stack.Push(500);
      
      Console.WriteLine("Original Stack elements:");
      foreach(int val in stack) {
         Console.WriteLine(val);
      }
      
      Console.WriteLine("\nCopying stack to array...");
      int[] intArr = new int[stack.Count];
      stack.CopyTo(intArr, 0);
      
      Console.WriteLine("Array after CopyTo():");
      for(int i = 0; i < intArr.Length; i++) {
         Console.WriteLine($"intArr[{i}] = {intArr[i]}");
      }
   }
}

The output of the above code is −

Original Stack elements:
500
400
300
200
100

Copying stack to array...
Array after CopyTo():
intArr[0] = 500
intArr[1] = 400
intArr[2] = 300
intArr[3] = 200
intArr[4] = 100

Using Stack.CopyTo() with String Elements

Example

using System;
using System.Collections;

public class Demo {
   public static void Main() {
      Stack stack = new Stack();
      stack.Push("First");
      stack.Push("Second");
      stack.Push("Third");
      stack.Push("Fourth");
      
      Console.WriteLine("Stack elements:");
      foreach(string val in stack) {
         Console.WriteLine(val);
      }
      
      Console.WriteLine("\nCopying to array starting at index 1...");
      string[] strArr = new string[6]; // Extra space for demonstration
      stack.CopyTo(strArr, 1);
      
      Console.WriteLine("Array contents after CopyTo():");
      for(int i = 0; i < strArr.Length; i++) {
         Console.WriteLine($"strArr[{i}] = {strArr[i] ?? "null"}");
      }
   }
}

The output of the above code is −

Stack elements:
Fourth
Third
Second
First

Copying to array starting at index 1...
Array contents after CopyTo():
strArr[0] = null
strArr[1] = Fourth
strArr[2] = Third
strArr[3] = Second
strArr[4] = First
strArr[5] = null

Common Use Cases

  • Data Export − Converting stack data to array format for serialization or file output.

  • Backup Creation − Creating array-based backups of stack contents for recovery purposes.

  • Algorithm Processing − Transferring stack data to arrays when algorithms require indexed access.

  • Performance Optimization − Converting to arrays when frequent random access is needed.

Conclusion

The Stack.CopyTo() method provides an efficient way to transfer all stack elements to an array while preserving the LIFO order. The method is particularly useful when you need to convert stack data to array format for processing, storage, or interoperability with other data structures.

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

155 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements