C# Generic Stack<T> Tutorial with Programming Example

In this chapter you will learn

  • What is Generic Stack<T> in C#?
  • How to Initialize Generic Stack<T> in C#?
  • Programming Example

What is Generic Stack<T> in C#?

Stack is based on Last In First Out [LIFO] mechanism. It is required when you need to access recently added item first. For example, all of you would have used Undo function. When you press Ctrl + Z then the last changes made by you reversed. It is an example of stack.

How to initialize Stack<T>?

Important Properties and Methods of Stack




  • Properties Description
    Count Returns the total count of elements in the Stack.
    Methods Description
    Push 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 Checks whether an item exists in the stack or not.
    Clear Removes all items from the stack.

    Programming Example

    Output

    Saturday | Friday | Thursday | Wednesday | Tuesday | Monday | Sunday |
    Top Item is : Saturday
    Removed Top Item of Stack : Saturday
    Now Stack’s Items are :
    Friday | Thursday | Wednesday | Tuesday | Monday | Sunday |

    _

    Summary

    In this tutorial you learned about Generic Stack&<T> In C# with programming example.


    One thought on “C# Generic Stack<T> Tutorial with Programming Example”

    1. thank you so much for this, i looked for 3 days to find a good tutorial and this is it. i copied every part and built a complete file system for this!! amazing work thank you again!!

    Leave a Reply

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