C# | Create a Stack from a collection
Last Updated :
01 Feb, 2019
Stack represents a
last-in, first out collection of object. It is used when you need a last-in, first-out access to items. When you add an item in the list, it is called
pushing the item and when you remove it, it is called
popping the item. Creation of stack means the addition of item into the stack.
Stack<T>.Push(Object) Method is used to Inserts an object at the top of the Stack.
Properties:
- The capacity of a Stack is the number of elements the Stack can hold. As elements are added to a Stack, the capacity is automatically increased as required through reallocation.
- If Count is less than the capacity of the stack, Push is an O(1) operation. If the capacity needs to be increased to accommodate the new element, Push becomes an O(n) operation, where n is Count. Pop is an O(1) operation.
- Stack accepts null as a valid value and allows duplicate elements.
Syntax:
public virtual void Push (object obj);
Parameter:
obj: The Object of type System.Object which is to push onto the Stack<T>. The value can be null.
Below given are some examples to understand the implementation in a better way :
Example 1:
CSHARP
// C# code to Create a Stack
// from a collection
using System;
using System.Collections.Generic;
class GFG {
// Driver code
public static void Main()
{
// Creating a Stack of strings
Stack<string> myStack1 = new Stack<string>();
// Inserting the elements into the Stack
myStack1.Push("GeeksforGeeks");
myStack1.Push("is");
myStack1.Push("the");
myStack1.Push("best");
myStack1.Push("website");
// Displaying the count of elements
// contained in the myStack1
Console.Write("Total number of elements in the Stack 1 are : ");
Console.WriteLine(myStack1.Count);
// Displaying the elements in Stack myStack1
foreach(string str in myStack1)
{
Console.WriteLine(str);
}
// Creating a Stack from a collection
Stack<string> myStack2 = new Stack<string>(myStack1.ToArray());
// Displaying the count of elements
// contained in the myStack2
Console.Write("Total number of elements in the Stack 2 are : ");
Console.WriteLine(myStack2.Count);
// Displaying the elements in Stack myStack2
foreach(string str in myStack2)
{
Console.WriteLine(str);
}
}
}
Output:
Total number of elements in the Stack 1 are : 5
website
best
the
is
GeeksforGeeks
Total number of elements in the Stack 2 are : 5
GeeksforGeeks
is
the
best
website
Example 2:
CSHARP
// C# code to Create a Stack
// from a collection
using System;
using System.Collections.Generic;
class GFG {
// Driver code
public static void Main()
{
// Creating a Stack of Integers
Stack<int> myStack1 = new Stack<int>();
// Inserting the elements into the Stack
myStack1.Push(5);
myStack1.Push(10);
myStack1.Push(15);
myStack1.Push(20);
myStack1.Push(25);
// Displaying the count of elements
// contained in the myStack1
Console.Write("Total number of elements in the Stack 1 are : ");
Console.WriteLine(myStack1.Count);
// Displaying the elements in Stack myStack1
foreach(int i in myStack1)
{
Console.WriteLine(i);
}
// Creating a Stack from a collection
Stack<int> myStack2 = new Stack<int>(myStack1.ToArray());
// Displaying the count of elements
// contained in the myStack2
Console.Write("Total number of elements in the Stack 2 are : ");
Console.WriteLine(myStack2.Count);
// Displaying the elements in Stack myStack2
foreach(int i in myStack2)
{
Console.WriteLine(i);
}
}
}
Output:
Total number of elements in the Stack 1 are : 5
25
20
15
10
5
Total number of elements in the Stack 2 are : 5
5
10
15
20
25
Reference: https://docs.microsoft.com/en-us/dotnet/api/system.collections.stack.push?view=netframework-4.7.2
Similar Reads
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.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
C# | Convert Stack to array Stack represents a last-in, first out collection of object. It is used when you need a last-in, first-out access of items. When you add an item in the list, it is called pushing the item and when you remove it, it is called popping the item. Stack<T>.ToArray Method is used to copy a Stack<T
2 min read
Stack.Pop() Method in C# This method(comes under System.Collections namespace) is used to remove and returns the object at the top of the Stack. This method is similar to the Peek method, but Peek does not modify the Stack. Syntax: public virtual object Pop (); Return Value: It returns the Object removed from the top of the
2 min read
C# | Remove all objects from the Stack Stack represents a last-in, first out collection of object. It is used when you need a last-in, first-out access to items. When you add an item in the list, it is called pushing the item and when you remove it, it is called popping the item. Stack<T>.Clear Method is used to Removes all objects
3 min read