---
title: "What are the uses of stack and queue in C# and how to implements them in the C# program?"  
description: "What are the uses of stack and queue in C# and how to implements them in the C# program?"  
author: "Ethan Karla"  
published: 2021-08-26  
canonical: https://answers.mindstick.com/qa/93934/what-are-the-uses-of-stack-and-queue-in-c-sharp-and-how-to-implements-them-in-the-c-sharp-program  
category: "programming"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# What are the uses of stack and queue in C# and how to implements them in the C# program?

What are the uses of [stack](https://www.mindstick.com/blog/301746/why-is-stack-overflow-so-important-for-developers) and [queue](https://www.mindstick.com/forum/160388/describe-the-basic-features-and-use-cases-of-the-queue-and-stack-collections-in-c-sharp) in C# and how to implements them in the [C# program](https://www.mindstick.com/interview/23040/how-to-use-sealed-classes-in-the-c-sharp-program)?

## Answers

### Answer by Ravi Vishwakarma

In C#, Stack and Queue is a class that is used for storing data. Stack is stored data in LIFO (Last in first out) order and Queue is store data in FIFO (First in first out) order. **Stack**

```
using System;
using System.Collections;
public class Program
{
 public static void Main()
 {
  Stack stack=new Stack();
  stack.Push(45);
  stack.Push(12);
  stack.Push(125);
  stack.Push(102);
  foreach(var item in stack)
   Console.WriteLine(item);
 }
}
```

**Output**

```
102
125
12
45
```

In this example, we can see print the last value at the first and first value at last in descending order. This is print the last value as a first value and the second-last value as a second value. **\** **Queue**

```
using System;
using System.Collections;
public class Program
{
 public static void Main()
 {
  Queue queue=new Queue();
  queue.Enqueue(45);
  queue.Enqueue(12);
  queue.Enqueue(125);
  queue.Enqueue(102);
  foreach(var item in queue)
   Console.WriteLine(item);
 }
}
```

**Output**

```
45
12
125
102
```

In this example, we can see that all value is store in ascending order. The queue is worked on FIFO (first in first out). \


---

Original Source: https://answers.mindstick.com/qa/93934/what-are-the-uses-of-stack-and-queue-in-c-sharp-and-how-to-implements-them-in-the-c-sharp-program

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
