C# Stack - CopyTo() Method



The C# stack CopyTo() method is used to copy the Stack to an existing one-dimensional Array, starting at the specified array index.

The elements are copied onto the array in last-in-first-out (LIFO) order.

Syntax

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

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

Parameters

This method accepts the following parameters −

  • array: It is a zero-based one-dimensional array that is the destination of the element copied from the stack.
  • index: It represents the zero-based index in the array at which the copy begins.

Return value

This method does not return any value.

Example 1: Using the CopyTo() Method

Following is the basic example of the CopyTo() method to copy the element of the stack in the array −

    
using System;
using System.Collections;
class Program {
   static void Main() {
      Stack myStack = new Stack();
      myStack.Push("Tutorialspoint");
      myStack.Push("Hyderabad");
      myStack.Push("India");
      myStack.Push("Tutorix");

      // Create an array with sufficient size
      object[] myArray = new object[5];

      // Copy stack elements to the array starting at index 2
      myStack.CopyTo(myArray, 1);

      // Display the array elements
      Console.WriteLine("Array after copying:");
      foreach (var item in myArray) {
         // Using ?? "null" to handle empty slots
         Console.WriteLine(item ?? "null");  
      }
   }
}

Output

Following is the output −

Array after copying:
null
Tutorix
India
Hyderabad
Tutorialspoint

Example 2: Convert a Stack into an Array

Let's see another example of the CopyTo() method to copy all the elements of the stack into the array −

using System;
using System.Collections;
class Program {
   static void Main() {
      Stack myStack = new Stack();
      myStack.Push(1);
      myStack.Push(2);
      myStack.Push(3);
      myStack.Push(4);
      myStack.Push(5);

      // Create an array with sufficient size
      object[] myArray = new object[5];

      // Copy stack elements to the array starting at index 2
      myStack.CopyTo(myArray, 0);

      // Display the array elements
      Console.WriteLine("Array after copying:");
      foreach (var item in myArray) {
         // Using ?? "null" to handle empty slots
         Console.WriteLine(item ?? "null");  
      }
   }
}

Output

Following is the output −

Array after copying:
5
4
3
2
1

Example 3: Copy The Person Details Into an Array

The below example creates a person object. Here, we use the CopyTo() method to create an array of person objects from a stack −

using System;
using System.Collections.Generic;
class Person {
   public string Name { get; set; }
   public int Age { get; set; }
   
   // Constructor to initialize name and age
   public Person(string name, int age) {
      Name = name;
      Age = age;
   }
   
   public override string ToString() {
      return $"Name: {Name}, Age: {Age}";
   }
}
class Example {
   static void Main() {
      // Create a stack of Person objects
      Stack<Person> people = new Stack<Person>();
      
      // Push Person objects onto the stack
      people.Push(new Person("Aman", 25));
      people.Push(new Person("Gupta", 24));
      people.Push(new Person("Vivek", 26));
      
      Console.WriteLine("People stack before CopyTo:");
      foreach (var person in people) {
         Console.WriteLine(person);
      }
      
      // Create an array to hold the copied elements
      Person[] peopleArray = new Person[3];
      
      people.CopyTo(peopleArray, 0);
      
      Console.WriteLine("\nArray after CopyTo:");
      foreach (var person in peopleArray) {
         Console.WriteLine(person);
      }
   }
}

Output

Following is the output −

People stack before CopyTo:
Name: Vivek, Age: 26
Name: Gupta, Age: 24
Name: Aman, Age: 25

Array after CopyTo:
Name: Vivek, Age: 26
Name: Gupta, Age: 24
Name: Aman, Age: 25
csharp_stack.htm
Advertisements