1. Stack Definition
The System. Collections. Stack class indicates a simple post-first-out non-generic set of objects.
2. Advantages
1. A collection of later, first, and foremost.
2. Stack can accept null references as valid values and allow repeated Elements
3. Stack Constructor
Constructors
Note
Stack ()
Initialize a new instance of the Stack class. The instance is empty and has the default initial capacity.
Stack (ICollection)
Initializes a new instance of the Stack class. The instance contains elements copied from the specified set and has the same initial capacity as the number of copied elements.
Stack (Int32)
Initialize a new instance of the Stack class. The instance is empty and has the specified initial capacity or default initial capacity (larger of the two capacities ).
4. Stack attributes
Attribute name
Note
Count
Obtains the number of elements contained in a Stack.
5. Stack Method
Method Name
Note
Void Clear ()
Remove all objects from the Stack.
Bool Contains (object obj)
Determine whether an element is in the Stack.
Object Clone ()
Create a superficial copy of the Stack.
Void CopyTo (Array array, int index)
Copy the Stack from the specified Array index to an existing one-dimensional Array.
Object Pop ()
Remove and return the object at the top of the Stack.
Void Push (object obj)
Insert the object to the top of the Stack.
Object Peek ()
Returns the object at the top of the Stack but does not remove it.
Object [] ToArray ()
Copy the Stack to the new array.
6. Example of Stack usage
Code
Class Program
{
Public static void Main ()
{
// Create a stack
Stack myStack = new Stack ();
MyStack. Push ("The"); // inbound Stack
MyStack. Push ("quick ");
MyStack. Push ("brown ");
MyStack. Push ("fox ");
// Print the value in the Set
Console. Write ("Stack values :");
PrintValues (myStack,