Stack.ToArray() Method in C# (original) (raw)
Last Updated : 04 Feb, 2019
This method(comes under System.Collections namespace) is used to copy a Stack to a new array. The elements are copied onto the array in last-in-first-out (LIFO) order, similar to the order of the elements returned by a succession of calls to Pop. This method is an O(n) operation, where n is Count.Syntax:
public virtual object[] ToArray ();
Return Type: This method returns a new array of type System.Object containing copies of the elements of the Stack. Below given are some examples to understand the implementation in a better way:Example 1:
CSHARP `
// C# code to illustrate the // Stack.ToArray() Method using System; using System.Collections;
class GFG {
// Driver code
public static void Main()
{
// Creating a Stack
Stack myStack = new Stack();
// Inserting the elements into the Stack
myStack.Push("Geeks");
myStack.Push("Geeks Classes");
myStack.Push("Noida");
myStack.Push("Data Structures");
myStack.Push("GeeksforGeeks");
// Converting the Stack into array
Object[] arr = myStack.ToArray();
// Displaying the elements in array
foreach(Object str in arr)
{
Console.WriteLine(str);
}
}
}
`
Output:
GeeksforGeeks Data Structures Noida Geeks Classes Geeks
Example 2:
CSHARP `
// C# code to illustrate the // Stack.ToArray() Method using System; using System.Collections;
class GFG {
// Driver code
public static void Main()
{
// Creating a Stack
Stack myStack = new Stack();
// Inserting the elements into the Stack
myStack.Push(2);
myStack.Push(3);
myStack.Push(4);
myStack.Push(5);
myStack.Push(6);
// Converting the Stack into array
Object[] arr = myStack.ToArray();
// Displaying the elements in array
foreach(Object i in arr)
{
Console.WriteLine(i);
}
}
}
`
Reference:
- https://docs.microsoft.com/en-us/dotnet/api/system.collections.stack.toarray?view=netframework-4.7.2
Similar Reads
- C# Stack Class In C#, the Stack class represents a Last-in-First-out (LIFO) collection of objects. The stack is the part of the System.Collections.Generic namespace. This class allows us to push elements onto the stack, pop elements from the stack, and peek at the top element without removing it.The capac 5 min read
- C# | How to create a Stack Stack() constructor is used to initialize a new instance of the Stack class which will be empty and will have the default initial capacity. Stack represents a last-in, first out collection of object. It is used when you need last-in, first-out access to items. When you add an item in the list, it is 2 min read
- Stack.Count Property in C# This method(comes under System.Collections namespace) is used to get the number of elements contained in the Stack. The capacity is the number of elements that the Stack can store and the count is the number of elements that are actually in the Stack. The capacity is always greater than or equal to 2 min read
- Stack.IsSynchronized Property in C# This method(comes under System.Collections namespace) is used to get a value indicating whether access to the Stack is synchronized (thread safe) or not. To guarantee the thread safety of the Stack, all operations must be done through the wrapper returned by the Synchronized method. Also, retrieving 2 min read
- How to get Synchronize access to the Stack in C# Stack.SyncRoot Property is used to get an object which can be used to synchronize access to the Stack. Stack represents last-in, first out collection of object. It is used when you need last-in, first-out access to items. When you add an item in the list, it is called pushing the item and when you r 3 min read
- Stack.Clear Method in C# This method(comes under System.Collections namespace) is used to remove all the objects from the Stack. This method will set the Count of Stack to zero, and references to other objects from elements of the collection are also removed. This method is an O(n) operation, where n is Count. Syntax: publi 2 min read
- Stack.Clone() Method in C# This method is used to create a shallow copy of the Stack. It just creates a copy of the Stack. The copy will have a reference to a clone of the internal data array but not a reference to the original internal data array. Syntax: public virtual object Clone (); Return Value: The method returns an Ob 2 min read
- Stack.Contains() Method in C# This method(comes under System.Collections namespace) is used to check whether a specified element is present is Stack or not. Internally this method checks for equality by calling the Object.Equals method. Also, it performs a linear search, therefore, this method is an O(n) operation, where n is Co 2 min read
- Stack.CopyTo() Method in C# This method(comes under System.Collections namespace) is used to copy the Stack to an existing 1-D Array which starts from the specified array index. The elements are copied onto the array in last-in-first-out (LIFO) order, similar to the order of the elements returned by a succession of calls to Po 2 min read
- Stack.Equals() Method in C# Equals(Object) Method which is inherited from the Object class is used to check if a specified Stack class object is equal to another Stack class object or not. This method comes under the System.Collections namespace. Syntax: public virtual bool Equals (object obj); Here, obj is the object which is 2 min read