C# Programming Language: Queue
Hi everyone, welcome back. In this tutorial, we will be going over the Queue and how to use the built in functions regarding them. A Queue is a linear type of data structure that is capable of storing objects. The Queue is a first-in-first-out or FIFO data structure which means that the first item added to the queue is the first item that will be removed. With this introduction out of the way, let’s get into it.

Creating The Queue
Let’s start by creating a Queue. Before we can create the Queue, we need to make sure we have our necessary imports, which will be included within the System.Collections.Generic package:
using System.Collections.Generic;
Now, we can successfully create our Queue:
Queue<int> myQueue = new Queue<int>();
So now, we have created our Queue variable of the ‘int’ data type and named it myQueue. When we create our Queue, we need to specify the data type. As we can see in the example above, we have used the ‘int’ data type. We can use other data types as well, such as the string or boolean data types.
Adding Elements to The Queue
Now, let’s see how we can add elements into our Queue. We can do this by using the built in Enqueue() function that is provided. Let’s see an example:
Queue<int> myQueue = new Queue<int>();
myQueue.Enqueue(1);
myQueue.Enqueue(2);
myQueue.Enqueue(3);
Console.WriteLine(String.Join(" ", myQueue));Output:
1 2 3
In the example above, we can see that we added a few elements into our Queue by using the Enqueue() function. We can verify that they were successfully added by printing our Queue and checking the output.
Accessing The First Element in The Queue
Now, let’s see how we can view the first element of the Queue. We can do this by using the built in Peek() function that is provided. Let’s see an example:
Queue<int> myQueue = new Queue<int>();
myQueue.Enqueue(1);
myQueue.Enqueue(2);
myQueue.Enqueue(3);
Console.WriteLine(myQueue.Peek());Output:
1
In the example above, we can see that we used the Peek() function to retrieve the first element of our Queue. We can verify that we retrieved the value by printing the result and checking the output.
Removing The First Element From The Queue
Now, let’s see how we can remove the first element from the queue. We can do this by using the built in Dequeue() function that is provided. The Dequeue() function will also return the first element of the queue. Let’s see an example:
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
In the example above, we can see that we have added a few elements into our Queue. We used the Dequeue() function to return and remove the first element in our Queue. We can verify that the Dequeue() function returns the first element by printing the result and checking the output. We can also verify that the Dequeue() function successfully removed the first element by printing the Queue and noting that the first element is no longer there.
Clearing Out The Queue
Now, let’s see how we can completely clear out the Queue. We can do this by using the built in Clear() function that is provided. Let’s see an example:
Queue<int> myQueue = new Queue<int>();
myQueue.Enqueue(1);
myQueue.Enqueue(2);
myQueue.Enqueue(3);
myQueue.Clear();
Console.WriteLine(String.Join(" ", myQueue));Output:
In the example above, we have added a few elements into our Queue. Then we used the Clear() function on our Queue. We can verify that all elements were removed by printing our Queue and checking the output. Our output should be empty because we removed everything.
Checking Elements in a Queue
Now, let’s see how we can check if an element exists in a Queue. We can do this by using the built in Contains() function that is provided. We will need to specify the value of the element to check for. Let’s see an example:
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
In the example above, we can see that we added a few numbers into our Queue. Then we printed the results of the Contains() function. We checked to see if our Queue contained the value ‘2’ and the value ‘4.’ As we can see in the output, our Queue contains a value of ‘2,’ but not ‘4.’
Checking The Size of a Queue
Now, let’s see how we can check the size of a Queue, or how many elements are in the Queue. We can do this by using the built in Count property that is provided. Let’s see an example:
Queue<int> myQueue = new Queue<int>();
myQueue.Enqueue(1);
myQueue.Enqueue(2);
myQueue.Enqueue(3);
Console.WriteLine(myQueue.Count);Output:
3
In the example above, we can see that we added a few numbers into our Queue. We then printed the result of the Count property on our Queue. As we can see in the output, our Queue contains 3 elements.
Conclusion
That is it for the C# Queue examples. We covered how to create Queues along with some of the functions regarding them. I hope this helps. If there are any questions or comments, please let me know. Thanks for reading.