Ad space (banner)
#๏ธโƒฃC# Lessons
Lesson 30 / 68

Stacks and Queues (Basic Data Structures)

In this lesson you will learn how to use stacks and queues in C#, and understand which way of adding and removing data suits which job. This is for anyone searching for "C# Stack Queue how to use".

Stack<T> is "last in, first out" and Queue<T> is "first in, first out". The difference between a pile of books and a queue of people is a helpful picture.

The sample code pushes "A" then "B" with stack.Push() and shows that stack.Pop() returns the last one pushed, "B"; meanwhile items joined with queue.Enqueue() come back out through queue.Dequeue() starting with the first one, "A".

A common stumbling block for beginners is that the two structures hand items back in opposite orders. Which one you pick changes the order of processing and the efficiency of your algorithm considerably.

In real-world development, the mechanism of function calls is itself managed with a stack, so this is an important structure that also builds your computer science foundations.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run C# directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)