1/19
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai | Chat |
|---|
No analytics yet
Send a link to your students to track their progress
Stack
A generic collection in System.Collections.Generic that stores elements for last-in, first-out (LIFO) processing. Domain: C# → .NET → System.Collections.Generic → Stack
LIFO
Last In, First Out; the most recently added element in a Stack is the next element removed.
Top of a Stack
The end of a Stack where elements are added, inspected, and removed.
Bottom of a Stack
The end of a Stack containing the elements that have been stored the longest.
new Stack
Creates an empty Stack
new Stack
Creates a Stack
stack.Count
Gets the number of elements currently contained in the Stack.
stack.Push(item)
Adds an element to the top of the Stack.
stack.Pop()
Removes and returns the element at the top of the Stack; throws InvalidOperationException if the Stack is empty.
stack.Peek()
Returns the element at the top of the Stack without removing it; throws InvalidOperationException if the Stack is empty.
stack.TryPop(out result)
Attempts to remove and return the top element, returning false if the Stack is empty.
stack.TryPeek(out result)
Attempts to return the top element without removing it, returning false if the Stack is empty.
stack.Contains(item)
Determines whether the specified element occurs in the Stack.
stack.Clear()
Removes all elements from the Stack.
stack.ToArray()
Copies the Stack elements into a new array with the top element first.
stack.TrimExcess()
Reduces excess internal storage capacity when appropriate relative to the number of elements currently stored.
Push vs Pop
Push adds an element to the top of a Stack, whereas Pop removes and returns the element from the top.
Pop vs Peek
Pop returns and removes the top element, whereas Peek returns the top element without removing it.
TryPop vs Pop
TryPop safely reports failure when the Stack is empty, whereas Pop throws an InvalidOperationException.
Stack
Stack