Introduction: Stack in C#.Net with example

In this post, we will learn about Stack in C#.Net with example.Stack follows a First-in, Last-out process. The basic concept of a stack can be understood by thinking situation of your data as a stack of plates or books where you can only take the top item off the stack in order to remove things from it.

Description:

In my previous posts I have explained various topics like HashSet, C# Collection Get site visitor count in asp.net. Convert generic list to datatable in Asp.Net C#.Net, Abstract Class in C# with Example, Routing in Asp.Net MVC with Example,Use of Global.asax in Asp.net
Now in this post, I will explain about Stack in C#.Net with example.

 

Useful Methods of Stack Class:

Some of the most and useful methods of stack are:

Push() :

This method is used to add an element in the stack.It insert element at the top of the Stack.

Pop() :

This method is used to remove the element from the stack or we can say that this method removes and returns the object at the top of the Stack.

Peek() :

This method returns the element at the top of the Stack without removing it.

Contain() :

It determines whether an element is in the Stack.

Clear() :

This method removes all objects from the Stack.

Example:

Now Create Console Application  in Visual Studio and write below lines of codes.

using System;
using System.Collections;

namespace ConsoleDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Stack _Color = new Stack();

            _Color.Push("Red");
            _Color.Push("Pink");
            _Color.Push("Blue");

            Console.WriteLine("Current stack: ");
            foreach (string c in _Color)
            {
                Console.Write(c + " ");
            }

            Console.WriteLine("\n\nAdd 2 new color to stack: ");

            Console.WriteLine();

            _Color.Push("White");
            _Color.Push("Gray");
            Console.WriteLine("\nCurrent stack: ");
            foreach (string c in _Color)
            {
                Console.Write(c + " ");
            }
            Console.WriteLine("\n\nThe next poppable value in stack: {0}", _Color.Peek());
            Console.WriteLine("Current stack: ");
            foreach (string c in _Color)
            {
                Console.Write(c + " ");
            }

            Console.WriteLine();

            Console.WriteLine("\n\nRemoving values ");
            _Color.Pop();

            Console.WriteLine("\n\nCurrent stack: ");
            foreach (string c in _Color)
            {
                Console.Write(c + " ");
            }

            Console.ReadKey();
        }
    }
}

Stack in C#.Net with example

stack

I hope you got an idea about Stack in C#.Net with example.
I would like to have feedback from my blog readers.
Your valuable feedback, question, or comments about this article are always welcome.

Leave a Reply

Your email address will not be published. Required fields are marked *

Name *
Email *
Website