Stack<T> Class
Summary
Represents a collection of objects that can be accessed by index. Each property in this type can be used to access a list of objects from the collection. The base class for all typed collections in the System.Collections.Generic namespace.
Remarks
The Stack<T> class represents a LIFO (Last-In, First-Out) collection of objects. The most recently added object is the only one that can be accessed.
Stack<T> implements the IEnumerable<T> and ICollection<T> interfaces. It also supports the generic ICollection<T> interface.
If the capacity is insufficient, the capacity is increased by reallocating the underlying array. The capacity doubles in size when reallocation is needed.
The elements in the stack are ordered from the top element, which is the last element added, to the bottom element, which is the first element added.
Members
| Name | Description |
|---|---|
| Push (T item) | Adds an object to the top of the Stack<T>. |
| Pop () | Removes and returns the object at the top of the Stack<T>. |
| Peek () | Returns the object at the top of the Stack<T> without removing it. |
| Count get; | Gets the number of elements contained in the Stack<T>. |
| IsReadOnly get; | Gets a value indicating whether the Stack<T> is read-only. |
| Clear () | Removes all objects from the Stack<T>. |
| Contains (T item) | Determines whether an element is in the Stack<T>. |
| CopyTo (T[] array, int arrayIndex) | Copies the Stack<T> to an array, starting at the specified array index. |
Constructors
-
Stack()
Initializes a new instance of the Stack<T> class that is empty, has the default initial capacity, and uses the default equality comparer for the type of elements that are in the list.
-
Stack(int capacity)
Initializes a new instance of the Stack<T> class that is empty, has the specified initial capacity, and uses the default equality comparer for the type of elements that are in the list.
-
Stack(IEnumerable<T> collection)
Initializes a new instance of the Stack<T> class that contains elements copied from the specified collection and has sufficient capacity to accommodate the number of elements copied.
Methods
-
Push
(T item)
Adds an object to the top of the Stack<T>.
-
Pop
()
Removes and returns the object at the top of the Stack<T>. Throws InvalidOperationException if the stack is empty.
-
Peek
()
Returns the object at the top of the Stack<T> without removing it. Throws InvalidOperationException if the stack is empty.
-
Clear
()
Removes all objects from the Stack<T>.
-
Contains
(T item)
Determines whether an element is in the Stack<T>.
-
CopyTo
(T[] array, int arrayIndex)
Copies the Stack<T> to an array, starting at the specified array index. The order of elements in the array is from the top of the stack to the bottom.
-
GetEnumerator
()
Returns an enumerator that iterates through the Stack<T>. The enumerator returns elements from the top of the stack to the bottom.
Properties
-
Count
get;
Gets the number of elements contained in the Stack<T>.
-
IsReadOnly
get;
Gets a value indicating whether the Stack<T> is read-only.
Syntax
using System;
using System.Collections.Generic;
public class Example
{
public static void Main()
{
// Create a new stack of strings
Stack<string> myStack = new Stack<string>();
// Push elements onto the stack
myStack.Push("Hello");
myStack.Push("World");
myStack.Push("!");
// Peek at the top element
Console.WriteLine($"Top element is: {myStack.Peek()}"); // Output: Top element is: !
// Pop elements from the stack
while (myStack.Count > 0)
{
Console.WriteLine($"Popped: {myStack.Pop()}");
}
// Output:
// Popped: !
// Popped: World
// Popped: Hello
}
}
Example
The following example demonstrates how to use the Stack<T> class to store and retrieve string objects. It shows how to push elements onto the stack, peek at the top element, and pop elements off the stack.
using System;
using System.Collections.Generic;
public class StackExample
{
public static void Main(string[] args)
{
// Create a stack of integers
Stack<int> numberStack = new Stack<int>();
// Push some numbers onto the stack
numberStack.Push(10);
numberStack.Push(20);
numberStack.Push(30);
Console.WriteLine($"Current stack count: {numberStack.Count}"); // Output: Current stack count: 3
// Peek at the top element
int topElement = numberStack.Peek();
Console.WriteLine($"Top element: {topElement}"); // Output: Top element: 30
// Pop elements from the stack
Console.WriteLine("Popping elements:");
while (numberStack.Count > 0)
{
int poppedValue = numberStack.Pop();
Console.WriteLine($"Popped: {poppedValue}");
}
// Output:
// Popping elements:
// Popped: 30
// Popped: 20
// Popped: 10
Console.WriteLine($"Stack count after popping: {numberStack.Count}"); // Output: Stack count after popping: 0
// Check if the stack contains a specific element
numberStack.Push(50);
bool contains50 = numberStack.Contains(50);
Console.WriteLine($"Does the stack contain 50? {contains50}"); // Output: Does the stack contain 50? True
// Clear the stack
numberStack.Clear();
Console.WriteLine($"Stack count after clearing: {numberStack.Count}"); // Output: Stack count after clearing: 0
}
}