Home

Search

C# Queue Collection: Enqueue

Use the Queue generic collection. Call the Enqueue method to add elements.

Queue. What is requested first is handled first. Queue is a FIFO collection. It processes elements in a first-in, first-out order.Stack

To restate, Queue handles the elements that it received longest ago first. We look at several examples of the Queue generic class.Generic Class, Method

Enqueue example. First we add 4 ints to an instance of Queue. These could correspond to requests. We add the new requests to the end of the Queue, because they will be dealt with last.Int, uint
First-In-First-Out: The queue data structure implements this algorithm. Queue is a generic type with one type parameter.
Enqueue: This method adds a value (of the Queue's specified element type) to the end of the Queue.
C# program that uses Enqueue using System; using System.Collections.Generic; class Program { static void Main() { // Create a new Queue of integers. var elements = new Queue<int>(); // Add 5 to the end. elements.Enqueue(5); // Add 10 to the queue, so 10 is now at the start. elements.Enqueue(10); // Continue adding to the start. elements.Enqueue(15); elements.Enqueue(20); // Write count of elements. Console.WriteLine("QUEUE COUNT: {0}", elements.Count); } } Output QUEUE COUNT: 4

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.
Foreach
C# program that loops with Queue using System; using System.Collections.Generic; class Program { static void Main() { // 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); } } } Output 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.
Simulation: 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.
C# program that uses Queue for request system using System; using System.Collections.Generic; class Program { enum RequestType { MouseProblem, TextProblem, ScreenProblem, ModemProblem }; static void Main() { // Initialize help requeust 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.For
CopyTo: We use CopyTo with an int array. We allocate the number of elements to the int[] with the same integer as the Queue's Count.
For: We use the for-loop syntax, which gives more power when iterating over the arrays.
Int Array
Results: The array is written to the Console in front to back order, and then it is written in the opposite direction.
Console
C# program that copies Queue using System; using System.Collections.Generic; class Program { static void Main() { // 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]); } } } Output 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
C# program that uses Contains on Queue using System; using System.Collections.Generic; class Program { static void Main() { var elements = new Queue<string>(); elements.Enqueue("bird"); // Search Queue with Contains. if (elements.Contains("bird")) { Console.WriteLine("CONTAINS: BIRD"); } } } Output CONTAINS: BIRD

Notes, performance. The performance of Queue, and the List.Insert method, should be tested. Using Insert on a List in the wrong way can hurt performance.Insert

A summary. We saw ways to Enqueue and Dequeue elements. With Queue we could process requests on a website. This collection is useful for certain situations.

Home
© 2007-2020 Sam Allen. Send bug reports to info@dotnetperls.com.