Queue ExamplesUse the Queue generic collection, calling the Enqueue method to add elements.
This page was last reviewed on Jan 22, 2024.
Queue. In a queue, what is requested first is handled first. In C# the Queue type is a FIFO collection. It processes elements in a first-in, first-out order.
Queue handles the elements that it received longest ago first. To further understand Queue, it is helpful to examine some uses of this generic class.
Enqueue example. Here we demonstrate the use of the Enqueue method on the Queue. Enqueue adds to the end of a Queue. We often call it multiple times.
Step 1 First we add 4 ints to an instance of Queue. These could correspond to requests.
Step 2 We print out the total count of elements in the Queue. We added 4 ints, so there are 4 elements.
Tip With Enqueue, we add the new requests to the end of the Queue, because they will be dealt with last.
int, uint
using System; using System.Collections.Generic; // Step 1: create a new Queue of integers. var elements = new Queue<int>(); elements.Enqueue(5); elements.Enqueue(10); elements.Enqueue(15); elements.Enqueue(20); // Step 2: write count of elements. Console.WriteLine("QUEUE COUNT: {0}", elements.Count);
Loop example. Next we loop through the elements in the Queue. Frequently you will need to loop through the elements. We can do this is the standard way.
Tip The foreach-loop calls the Queue's enumerator, and you can use the standard foreach syntax.
Info Behind the scenes, the foreach statement is invoking the Queue's enumerator. The foreach syntax hides this.
using System; using System.Collections.Generic; // Add integers to queue. Queue<int> collection = new Queue<int>(); collection.Enqueue(5); collection.Enqueue(6); // Loop through queue. foreach (int value in collection) { Console.WriteLine(value); }
5 6
Help system. This code could be adapted into a help request processing system. You don't want your company's visitors to end up waiting forever. The Queue ensures this won't happen.
Info This example simulates a system where new help RequestTypes are added to the Queue. New requests are the last to be processed.
Tip The oldest requests, those that have been waiting the longest, are the closest to being acted on. No one is left hanging infinitely.
using System; using System.Collections.Generic; class Program { enum RequestType { MouseProblem, TextProblem, ScreenProblem, ModemProblem }; static void Main() { // Initialize help request Queue. Queue<RequestType> help = new Queue<RequestType>(); // Website records a Text Problem help request. help.Enqueue(RequestType.TextProblem); // Website records a Screen Problem help request. help.Enqueue(RequestType.ScreenProblem); // You become available to take a new request. // ... You can't help with Mouse problems. if (help.Count > 0 && help.Peek() != RequestType.MouseProblem) { // You solve the request. // ... It was a TextProblem. help.Dequeue(); } // Record a Modem Problem help request. help.Enqueue(RequestType.ModemProblem); } }
Copy. We can copy Queue elements. We might have a Queue collection but need to loop over the elements in the reverse order. We call CopyTo, and then loop over the array in reverse.
Here We use CopyTo with an int array. We allocate the number of elements to the int array with the same integer as the Queue's Count.
Next We use the for-loop syntax, which gives more power when iterating over the arrays.
int Array
Result The array is written to the Console in front to back order, and then it is written in the opposite direction.
using System; using System.Collections.Generic; // New Queue of integers. Queue<int> queue = new Queue<int>(); queue.Enqueue(5); queue.Enqueue(10); queue.Enqueue(15); queue.Enqueue(20); // Create new array with Length equal to Queue's element count. int[] array = new int[queue.Count]; // Copy the Queue to the int array. queue.CopyTo(array, 0); // Loop through and display int[] in order. Console.WriteLine("Array:"); for (int i = 0; i < array.Length; i++) { Console.WriteLine(array[i]); } // Loop through int array in reverse order. Console.WriteLine("Array reverse order:"); for (int i = array.Length - 1; i >= 0; i--) { Console.WriteLine(array[i]); }
Array: 5 10 15 20 Array reverse order: 20 15 10 5
Contains. The Contains() method works in the same way as the List Contains method. It searches iteratively through the queue. The Queue does not use hashing so the search takes linear time.
List Contains
using System; using System.Collections.Generic; var elements = new Queue<string>(); elements.Enqueue("bird"); // Search Queue with Contains. if (elements.Contains("bird")) { Console.WriteLine("CONTAINS: BIRD"); }
The performance of Queue, and the List.Insert method, should be tested. Using Insert on a List in the wrong way can hurt performance.
List Insert
Summary. In .NET the Queue generic class provides methods to add (enqueue) and remove elements. With Queue we could process requests. This collection is useful for certain situations.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Jan 22, 2024 (edit).
© 2007-2024 Sam Allen.