- 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 - ToArray() Method
The C# stack ToArray() method is used to copy the stack into a new array. Elements are copied to the array in a last-in-first-out (LIFO) order, similar to the order of elements returned by the pop method. This method takes O(n) operation, where n is the number of elements or count.
Syntax
Following is the syntax of the C# stack ToArray() method −
public virtual object?[] ToArray ();
Parameters
This method does not accepts any parameters.
Return value
This method returns a new array containing copies of the elements of the Stack.
Example 1: Copying The Stack onto An Array
Following is the basic example to demonstrate the use of ToArray() method −
using System;
using System.Collections.Generic;
class Program {
static void Main() {
Stack<string> stack = new Stack<string>();
stack.Push("First");
stack.Push("Second");
stack.Push("Third");
stack.Push("Fourth");
// Convert stack to array
string[] array = stack.ToArray();
// Display the array elements
Console.WriteLine("Array Elements:");
foreach (var item in array) {
Console.WriteLine(item);
}
}
}
Output
Following is the output −
Array Elements: Fourth Third Second First
Example 2: Processing Stack Data
This example demonstrates how to convert a stack to an array and then perform operations on the array without modifying the original stack −
using System;
using System.Collections.Generic;
class Program {
static void Main() {
Stack<int> numbers = new Stack<int>();
numbers.Push(10);
numbers.Push(20);
numbers.Push(30);
numbers.Push(40);
numbers.Push(50);
// Convert stack to array
int[] numArray = numbers.ToArray();
// Display the array elements
Console.WriteLine("Array Elements (Converted from Stack):");
foreach (var num in numArray) {
Console.Write(num + " ");
}
// Finding the sum of elements in the array
int sum = 0;
foreach (var num in numArray) {
sum += num;
}
Console.WriteLine("\n\nSum of Array Elements: " + sum);
}
}
Output
Following is the output −
Array Elements (Converted from Stack): 50 40 30 20 10 Sum of Array Elements: 150
Example 3: Reversing a Sentence
This example demonstrates how to use a stack to reverse the words in a sentence and then convert the stack into an array −
using System;
using System.Collections.Generic;
class Example3 {
static void Main() {
string sentence = "C# Stack ToArray Example";
Stack<string> wordsStack = new Stack<string>();
// Split the sentence into words and push them onto the stack
foreach (string word in sentence.Split(' '))
{
wordsStack.Push(word);
}
// Convert stack to array
string[] reversedWords = wordsStack.ToArray();
// Display reversed sentence
Console.WriteLine("Reversed Sentence: " + string.Join(" ", reversedWords));
}
}
Output
Following is the output −
Reversed Sentence: Example ToArray Stack C#