- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Arithmetic Operators
- C# - Assignment Operators
- C# - Relational Operators
- C# - Logical Operators
- C# - Bitwise Operators
- C# - Miscellaneous Operators
- C# - Operators Precedence
- C# Conditional Statements
- C# - Decision Making
- C# - If
- C# - If Else
- C# - Nested If
- C# - Switch
- C# - Nested Switch
- C# - Switch Expressions
- C# Control Statements
- C# - Loops
- C# - For Loop
- C# - While Loop
- C# - Do While Loop
- C# - Nested Loops
- C# - Break
- C# - Continue
- C# - Foreach Loop
- C# - Goto Statement
- C# OOP & Data Handling
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - LINQ
- C# - IEnumerable vs IEnumerator
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Tasks and Parallel Programming
- C# - Multithreading
- C# - Extension Methods
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