C# Programming Language: Queue

Creating The Queue

using System.Collections.Generic;
Queue<int> myQueue = new Queue<int>();

Adding Elements to The Queue

Queue<int> myQueue = new Queue<int>();
myQueue.Enqueue(1);
myQueue.Enqueue(2);
myQueue.Enqueue(3);
Console.WriteLine(String.Join(" ", myQueue));
Output:
1 2 3

Accessing The First Element in The Queue

Queue<int> myQueue = new Queue<int>();
myQueue.Enqueue(1);
myQueue.Enqueue(2);
myQueue.Enqueue(3);
Console.WriteLine(myQueue.Peek());
Output:
1

Removing The First Element From The Queue

Queue<int> myQueue = new Queue<int>();
myQueue.Enqueue(1);
myQueue.Enqueue(2);
myQueue.Enqueue(3);
Console.WriteLine(myQueue.Dequeue());
Console.WriteLine(String.Join(" ", myQueue));
Output:
1
2 3

Clearing Out The Queue

Queue<int> myQueue = new Queue<int>();
myQueue.Enqueue(1);
myQueue.Enqueue(2);
myQueue.Enqueue(3);
myQueue.Clear();
Console.WriteLine(String.Join(" ", myQueue));
Output:

Checking Elements in a Queue

Queue<int> myQueue = new Queue<int>();
myQueue.Enqueue(1);
myQueue.Enqueue(2);
myQueue.Enqueue(3);
Console.WriteLine(myQueue.Contains(2));
Console.WriteLine(myQueue.Contains(4));
Output:
True
False

Checking The Size of a Queue

Queue<int> myQueue = new Queue<int>();
myQueue.Enqueue(1);
myQueue.Enqueue(2);
myQueue.Enqueue(3);
Console.WriteLine(myQueue.Count);
Output:
3

Conclusion

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Write Your Code for Other Programmers

GEE Tutorial #23: How to search Earth Engine API and import assets from GEE personal account?

Starting my journey to become a front-end developer

Firebase Implementation Tutorial

The Metaverse Revolution — Meet Our Advisors Series: Aaron Lee-Zucker

GoARK — Let’s GO Towards New Horizons

Challenges of integrating with third party API’s of corporate companies and how to handle them

Chrome Extensions to Boost Your Productivity

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Jesse L

Jesse L

More from Medium

C# Programming Language: SortedList

Async programming vs Parallel programming (Basic)

C# extension methods

Using ‘var’ in C# — a pragmatic approach