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

  • PropertiesDescription
    CountReturns the total count of elements in the Stack.
    MethodsDescription
    PushInserts an item at the top of the stack.
    PeekReturns the top item from the stack.
    PopRemoves and returns items from the top of the stack.
    ContainsChecks whether an item exists in the stack or not.
    ClearRemoves 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.


    Leave a Reply

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