Topic : Csharp Collection - Stack in C# Programming Language:

Stack:

C# Includes a special type of Collection which stores elements in LIFO Style(List in First Out). Its behaves like a real stack of plates or cards where we operate it in a LIFO context, mean Last-In-First-Out. We can only insert or extract element from one end of the container. Whenever We need to added an element in stack, it will goes on the top, and the only element we can remove that will be at the top of the stack.




Stack: infobrother

Let's take a real-world example to know about the Operations, we can perform on a stack. Let's suppose, there is a pile of 10 Books. and we want to add one more book. so how to do that? simply add the book on the TOP of the pile. How about if we want the 5th book from the pile? We need to remove first 4 books from the pile to get the 5th book Then we take the 5th book, and replace all the others back into the pile by adding them from the TOP.



Declaring Stack:

Stack Allows Null value and also duplicate values. We can Use The following syntax to declare stack:


Stack   MyStack = new Stack(); 


We use Stack, when we need a last-in and first-out access of items. When we add an Item in the List, it is called pushing the Item, and when we remove it, it is called popping the Item. The Stack Class Provide some useful methods and Properties to work with stack Collection. Following are the most commonly used Properties of the Stack class:



PropertiesDescription
CountGets the number of elements contained in the Stack.


MethodsDescription
Clear()Removes all objects from the Stack.
Peek()Returns the object at the top of the Stack without removing it.
Pop()Removes and returns the object at the top of the Stack.
Push()Inserts an object at the top of the Stack.
ToArray()Copies the Stack to a new array.
For complete list of properties and methods, please visit Microsoft's C# documentation.


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. Let's have a simple example to know, how we can work with stack. how to add elements in it, how to remove elements from it and how to access stack elements.



/*Example - Stack Class - InfoBrother*/

using System;
using System.Collections;

namespace CollectionsApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            //Stack Create:
            Stack Stk = new Stack();

            //Add elements into Stack:
            Stk.Push("C#");
            Stk.Push("C++");
            Stk.Push("Java");
            Stk.Push("PHP");

            Console.WriteLine("Current Course in stack: ");
            //Retrieve Elements from Stack:
            foreach (string str in Stk)
            {
                Console.Write(str + " | ");
            }

            Console.WriteLine();

            //Add some more elements into stack:
            Stk.Push("CSS");
            Stk.Push("HTML");

            
            Console.WriteLine("Updated Course in stack: ");
            //Retrieve Elements from Stack:
            foreach (string str in Stk)
            {
                Console.Write(str + " | ");
            }

            //Get the top Element of the stack:
            Console.WriteLine("\n\nThe First Removable Course from stack is: {0}", Stk.Peek());
            Console.WriteLine("\nRemoving First Course: ");
            Stk.Pop();   //Remove the top element From the stack:

            Console.WriteLine("Remaining Course in stack are: ");
            //Retrieve Elements from Stack:
            foreach (String str in Stk)
            {
                Console.Write(str + " | ");
            }


            //Get the Top Element of the stack:
            Console.WriteLine("\n\nThe Next Removable Course from stack is: {0}", Stk.Peek());
            Console.WriteLine("\nRemoving Next Course: ");
            Stk.Pop();  //Remove the Top Element from the stack:

            Console.WriteLine("Remaining Course in stack are: ");
            //Retrieve Elements from Stack:
            foreach (String str in Stk)
            {
                Console.Write(str + " | ");
            }

            //Check the Element contain in the Stack:
            Console.WriteLine("\n\nDo We have CSS course in Our Stack? " + Stk.Contains("CSS"));
            Console.WriteLine("Do We have C# course in Our Stack? " + Stk.Contains("C#"));

            Console.ReadKey();
        }
    }
}


For complete list of properties and methods, please visit Microsoft's C# documentation.
















I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement