
- C# Basic Tutorial
- 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# - Decision Making
- C# - Loops
- 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# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
Copy the Stack to an Array in C#
To copy the stack to an array, the code is as follows −
Example
using System; using System.Collections.Generic; public class Demo { public static void Main(){ Stack<int> stack = new Stack<int>(); stack.Push(10); stack.Push(20); stack.Push(30); stack.Push(40); stack.Push(50); stack.Push(60); stack.Push(70); stack.Push(80); stack.Push(90); stack.Push(100); Console.WriteLine("Displaying the stack..."); foreach(int val in stack){ Console.WriteLine(val); } int[] intArr = new int[stack.Count]; stack.CopyTo(intArr, 0); Console.WriteLine("Displaying the array..."); foreach(int val in intArr){ Console.WriteLine(val); } } }
Output
This will produce the following output −
Displaying the stack... 100 90 80 70 60 50 40 30 20 10 Displaying the array... 100 90 80 70 60 50 40 30 20 10
Example
Let us now see another example −
using System; using System.Collections.Generic; public class Demo { public static void Main(){ Stack<int> stack = new Stack<int>(); stack.Push(10); stack.Push(20); stack.Push(30); stack.Push(40); stack.Push(50); Console.WriteLine("Displaying the stack..."); foreach(int val in stack){ Console.WriteLine(val); } int[] intArr = new int[10]; stack.CopyTo(intArr, 2); Console.WriteLine("Displaying the array..."); foreach(int val in intArr){ Console.WriteLine(val); } } }
Output
This will produce the following output −
Displaying the stack... 50 40 30 20 10 Displaying the array... 0 0 50 40 30 20 10 0 0 0
- Related Articles
- Java Program to copy an array from the specified source array
- How to reverse the elements of an array using stack in java?
- How to create a stack in TypeScript using an array?
- How to copy a List collection to an array?
- Copy all elements in Java TreeSet to an Object Array
- How to copy a section of an array into another array in C#?
- Check if an array is stack sortable in C++
- Convert Stack to array in C#
- Copy all elements of ArrayList to an Object Array in Java
- How to copy a specific section of an array in Java?
- Copy the entire LinkedList to Array in C#
- Copy all elements of Java HashSet to an Object Array
- Copy all elements of Java LinkedHashSet to an Object Array
- Array Copy in Java
- Array Copy in C#

Advertisements