Stack.Copyto() Method In C#

The CopyTo function in C# enables the duplication of elements from an array to another array or a defined position within the array. This method offers a simple approach to replicate the array's contents while maintaining identical values within a specified range. By defining the target array and initial index, users can effectively manage the element duplication procedure and streamline data and array manipulations.

Syntax:

It has the following syntax:

Example

public void CopyTo (T[] arr, int arrIndex);

Parameters:

  • array: It is a one-dimensional array that is the destination for elements copied from the stack. Arrays must have zero-based indices.
  • arrIndex: It is the 0-based index within the array where the copy begins
  • Exception:

  • ArgumentNullException: If the array is null.
  • ArgumentOutOfRangeException: If the index is less than 0.
  • ArgumentException: If the array is multidimensional or the number of elements in the source stack is greater than the available space from the index to the end of the target array.
  • InvalidCastException: If the source stack type cannot be automatically cast to the target array type.
  • Example 1:

Let's consider an example to demonstrate the utilization of the stack.CopyTo method in C#.

Example

using System;
using System.Collections;
public class StackCopy{
   public static void Main(){
      Stack st = new Stack();
      st.Push(115);
      st.Push(320);
      st.Push(560);
      st.Push(750);
      st.Push(1100);
      st.Push(1245);
      st.Push(1520);
      st.Push(2002);
      st.Push(2501);
      Console.WriteLine("The elements of the Stack are:");
      foreach(int value in st){
         Console.WriteLine(value);
      }
      Console.WriteLine("The total elements of the stack = "+st.Count);
      Console.WriteLine("Element 750 is the stack? = "+st.Contains(320));
      st.Push(3050);
      Console.WriteLine("The Updated Stack elements are");
      foreach(int value in st)
      {
         Console.WriteLine(value);
      }
      Console.WriteLine("Count of elements (updated) = "+st.Count);
      Console.WriteLine("Is 5050 is present in the stack? = "+st.Contains(5050));
      Stack st2 = (Stack)st.Clone();
      Console.WriteLine("The cloned stack elements");
      foreach(int value in st2){
         Console.WriteLine(value);
      }
      Console.WriteLine("The updated count of the stack elements= "+st2.Count);
      Console.WriteLine("The cloned elements are copied into the integer array");
      int[] intarray = new int[st2.Count];
      st2.CopyTo(intarray, 0);
      foreach(int j in intarray){
         Console.WriteLine(j);
      }
   }
}

Output:

Output

The elements of the Stack are:
2501
2002
1520
1245
1100
750
560
320
115
The total elements of the stack = 9
Element 750 is the stack? = True
The Updated Stack elements are
3050
2501
2002
1520
1245
1100
750
560
320
115
Count of elements (updated) = 10
Is 5050 is present in the stack? = False
The cloned stack elements
3050
2501
2002
1520
1245
1100
750
560
320
115
The updated count of the stack elements = 10
The cloned elements are copied into the integer array
3050
2501
2002
1520
1245
1100
750
560
320
115

Example 2:

Let's consider an example to demonstrate the implementation of the stack.CopyTo method in C#.

Example

using System;
using System.Collections;
public class StackCopy2{
   public static void Main(){
       //An empty stack created
      Stack st= new Stack();
      st.Push("Telugu");
      st.Push("Hindi");
      st.Push("English");
      st.Push("Maths");
      st.Push("Science");
      Console.WriteLine("The elements of the stack are......");
      foreach(string value in st){
         Console.WriteLine(value);
      }
      Console.WriteLine("The total count of elements= "+st.Count);
      Console.WriteLine("Is Maths is the stack? = "+st.Contains("Maths"));
      st.Push("Physics");
      Console.WriteLine("The updated.. elements of the stack are");
      foreach(string value in st){
         Console.WriteLine(value);
      }
      Console.WriteLine("Count of elements (updated) = "+st.Count);
      Console.WriteLine("Is the Physics element is in stack? = "+st.Contains("Physics"));
      Stack st2 = (Stack)st.Clone();
      Console.WriteLine("The cloned.. stack elements are");
      foreach(string value in st2){
         Console.WriteLine(value);
      }
      Console.WriteLine("The count of the updated stack = "+st2.Count);
      Console.WriteLine("The elements are cloned and copied in the string array.");
      string[] starray = new string[st2.Count];
      st2.CopyTo(starray, 0);
      foreach(string j in starray){
         Console.WriteLine(j);
      }
   }
}

Output:

Output

The elements of the stack are......
Science
Maths
English
Hindi
Telugu
The total count of elements= 5
Is Maths is the stack? = True
The updated.. elements of the stack are
Physics
Science
Maths
English
Hindi
Telugu
Count of elements (updated) = 6
Is the Physics element is in stack? = True
The cloned.. stack elements are
Physics
Science
Maths
English
Hindi
Telugu
The count of the updated stack = 6
The elements are cloned and copied in the string array.
Physics
Science
Maths
English
Hindi
Telugu

Advantages of Stack.CopyTo Method:

  • Efficient group setup:

In situations where duplicating the Stack's content to an array is necessary, using the CopyTo method can offer a more efficient solution than manually removing items from the Stack and transferring them to the array. This method provides a concise and straightforward approach to achieve the desired outcome.

  • Stack arrangement capability:

The CopyTo method maintains the sequence of items in the Stack by replicating the collection. This guarantees that the structure of elements in the copied collection remains unchanged, which can be crucial in certain scenarios.

  • Elaborate on a practical approach:

The approach allows for specifying an initial index in the target cluster, giving you flexibility in managing the duplication process. This enables you to precisely determine the placement of the elements being copied.

  • Cluster Compatibility:

When dealing with clusters in C#, the CopyTo method offers a practical approach to structuring data, aligning with standard coding practices. If you are managing clusters and transferring elements from the Stack, implementing this method can provide a familiar and consistent approach.

Input Required

This code uses input(). Please provide values below: