---
title: "Stack Class in C#"  
description: "The Stack class represents a Last-in- First –Out (LIFO) stack of objects."  
author: "Anupam Mishra"  
published: 2016-01-01  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/11977/stack-class-in-c-sharp  
category: "c#"  
tags: ["c#", "collection"]  
reading_time: 3 minutes  

---

# Stack Class in C#

The Stack class represents a Last-in- **First –Out (LIFO) stack of objects**. It is mainly two [operation](https://yourviews.mindstick.com/view/83916/why-is-it-necessary-to-sterilize-operation-theater) push and pop i.e. we can push (insert) items into the stack and pop (retrieve) items into the stack. Stack is implemented a circular buffer. It is follows the (Last-in-first-out) strategy. We can push the items into a stack and get into a [reverse order](https://www.mindstick.com/forum/156836/what-is-a-c-sharp-program-to-print-individual-from-a-string-in-reverse-order). Stack is returned as last elements as a first when you try to retrieve the items into the stack. As elements are added to a stack, the capacity is automatically increased as [required](https://yourviews.mindstick.com/view/81031/vigilence-required-during-corona-pandemic) through reallocation.

##### Stack Constructor:

**1. Stack ():** initializes a new [instance](https://www.mindstick.com/forum/155658/testing-connection-to-cloud-sql-instance-with-a-mysql-client-from-vm-instance) of the stack class i.e. empty and has the default initial capacity.

**2. Stack (ICollection):** Initializes a new [instances](https://answers.mindstick.com/qa/92508/how-can-you-hide-the-sql-server-instances) of the stack class that contains elements copied.

**3. Stack (Int 32):** initializes a new instances of the stack class i.e. empty and has the specified initial capacity or the default initial capacity, whichever is greater.

##### Stack Properties:

##### 1. Count: Gets the number of elements contained in the stack.

##### 2. IsSynchronized: Gets a value indicating whether access to the stack is

##### synchronized (thread safe).

##### Stack Methods:

Some of the important methods of stack class are:

**1.** **Clear ():** Removes all objects from the stack.

**2.** **Clone ():** Create a [shallow copy](https://www.mindstick.com/forum/158865/explain-the-difference-between-a-shallow-copy-and-a-deep-copy-in-python) of the stack.

**3.** **Contains (Object):** Determines whether an elements is in the stack.

**4.** **Peek ():** Returns the object at the top of the stack without removing it.

**5.** **Pop ():** Removes and returns the object at the top of the stack.

**6.** **Push (Object):** Inserts an object at the top of the stack.

Advantage of Stack:

**1.** As elements are added to the stack, the capacity of a stack is automatically increased as required through reallocation.

**2.** Stack accepts null as a valid value and allows [duplicate elements](https://www.mindstick.com/forum/158773/write-a-function-to-remove-duplicate-elements-from-a-list-in-python).

**3.** Also applicable [thread safety](https://www.mindstick.com/forum/159021/what-are-rust-s-mechanisms-for-ensuring-thread-safety-in-concurrent-programming) of the stack class.

For example, we create an instance of stack class with initial capacity by using its [default constructor](https://www.mindstick.com/forum/34531/default-constructor) and Inserting (Push) three values into the stack. After inserting elements we have retrieve (Pop) the elements.

```
using System;using System.Collections;namespace StackEx{    class Program    {        static void Main(string[] args)        {             Stack st = new Stack();       // Inserting(Pop) values into the stack            st.Push("Hi");            st.Push("Hello");            st.Push("Everyone");            Console.WriteLine(@"Let us try to use stack in the program ");            Console.WriteLine();            Console.WriteLine("Count: {0}",st.Count);            Console.WriteLine();            Console.WriteLine("Values:");            Console.WriteLine();            PrintValues(st);            Console.ReadKey();        }         // For retrieving(Pop) values from the stack        public static void PrintValues(IEnumerable MyCollection)        {            foreach (Object obj in MyCollection)            {                Console.Write("{0}",obj);                Console.WriteLine();            }        }    }} 
```

\

**Output:**

\

```

```

---

Original Source: https://www.mindstick.com/articles/11977/stack-class-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
