Stack
is a special type of collection that stores elements in LIFO style (Last In First Out). C# includes the generic Stack<T>
and non-generic Stack
collection classes. It is recommended to use the generic Stack<T>
collection.
Stack Pancake House
Stack is useful to store temporary data in LIFO style, and you might want to delete an element after retrieving its value.
Stack<T> Characteristics
Stack Overflow is the largest, most trusted online community for developers to learn, share their programming knowledge, and build their careers.
- STACK makes every team stronger – from subcontractors on the hunt for more profitable work to GCs and homebuilders looking for flexible takeoff and estimating tools to building product manufacturers and suppliers aiming to earn new business and build lasting customer relationships with.
- STACK is a multi-partner loyalty platform that brings all your reward points together, on an single platform that is powered by OCBC. With STACK, you can easily track and exchange your rewards points across the participating rewards programmes in real-time.
Stack<T>
is Last In First Out collection.- It comes under
System.Collection.Generic
namespace. Stack<T>
can contain elements of the specified type. It provides compile-time type checking and doesn't perform boxing-unboxing because it is generic.- Elements can be added using the
Push()
method. Cannot use collection-initializer syntax. - Elements can be retrieved using the
Pop()
and thePeek()
methods. It does not support an indexer.
Stacked Leggings
Creating a Stack
You can create an object of the Stack<T>
by specifying a type parameter for the type of elements it can store. The following example creates and adds elements in the Stack<T>
using the Push()
method. Stack allows null (for reference types) and duplicate values.
You can also create a Stack from an array, as shown below.
Stack<T> Properties and Methods:
Property | Usage |
---|---|
Count | Returns the total count of elements in the Stack. |
Method | Usage |
---|---|
Push(T) | Inserts an item at the top of the stack. |
Peek() | Returns the top item from the stack. |
Pop() | Removes and returns items from the top of the stack. |
Contains(T) | Checks whether an item exists in the stack or not. |
Clear() | Removes all items from the stack. |
Pop()
The Pop()
method returns the last element and removes it from a stack. If a stack is empty, then it will throw the InvalidOperationException
. So, always check for the number of elements in a stack before calling the Pop()
method.
4,3,2,1,
Number of elements in Stack: 0
Peek()
The Peek()
method returns the lastly added value from the stack but does not remove it. Calling the Peek()
method on an empty stack will throw the InvalidOperationException
. So, always check for elements in the stack before retrieving elements using the Peek()
method.
Stackmaster Cookware
Contains()
Stackable Washer And Dryer
The Contains()
method checks whether the specified element exists in a Stack collection or not. It returns true if it exists, otherwise false.
Field Summary
Fields inherited from class java.util.Vector
capacityIncrement, elementCount, elementData
Fields inherited from class java.util.AbstractList
modCount
Constructor Summary
Constructors Constructor and Description Stack()
Method Summary
All MethodsInstance MethodsConcrete Methods Modifier and Type Method and Description boolean
empty()
Tests if this stack is empty.E
peek()
Looks at the object at the top of this stack without removing it from the stack.E
pop()
Removes the object at the top of this stack and returns that object as the value of this function.E
push(E item)
int
search(Object o)
Returns the 1-based position where an object is on this stack.Methods inherited from class java.util.Vector
add, add, addAll, addAll, addElement, capacity, clear, clone, contains, containsAll, copyInto, elementAt, elements, ensureCapacity, equals, firstElement, forEach, get, hashCode, indexOf, indexOf, insertElementAt, isEmpty, iterator, lastElement, lastIndexOf, lastIndexOf, listIterator, listIterator, remove, remove, removeAll, removeAllElements, removeElement, removeElementAt, removeIf, removeRange, replaceAll, retainAll, set, setElementAt, setSize, size, sort, spliterator, subList, toArray, toArray, toString, trimToSize
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Collection
parallelStream, stream