C# Queue Tutorial

A Queue in C# represents a first-in, first-out (FIFO) collection of objects. An example of a queue is a line of people waiting.

The Queue<T> class in the System.Collection.Generic namespace represents a queue in C#, where T specifies the type of elements in the queue. In this article and code examples, I explain the use of Queue in C#, Queue class properties and methods, and how to use these methods in C#.
 

C# Queue Constructors

 
The Queue<T> class has several overloaded constructors.
 
Queue<T> Constructor - Initializes a new instance of the Queue<T> class that is empty. The next lines shows how we create the empty queue.
Queue<string> queue = newQueue<string>();  

Queue<T> Constructor (IEnumerable<T>) - Initializes a new instance of the Queue<T> class that contains elements copied from the specified collection.

string[] courses = { "MCA","MBA", "BCA","BBA", "BTech","MTech" };  
Queue<string> queue = newQueue<string>(courses);  

Queue<T> Constructor (Int32) - Initializes a new instance of the Queue<T> class that is empty and has the specified initial capacity.

Queue<string> queue = newQueue<string>(4);  

Queue.Count Property

The Count property gets the number of elements of Queue<T>. The following code sample creates a queue of strings and gets the item count in the collection.

namespace Queue {  
    classProgram {  
        staticvoid Main(string[] args) {  
            string[] courses = {  
                "MCA",  
                "MBA",  
                "BCA",  
                "BBA",  
                "BTech",  
                "MTech"  
            };  
            Queue < string > queue1 = newQueue < string > ();  
            Queue < string > queue2 = newQueue < string > (courses);  
            Queue < string > queue3 = newQueue < string > (4);  
            Console.WriteLine("Number of elements in queue1:" + queue1.Count());  
            Console.WriteLine("Number of elements in queue2:" + queue2.Count());  
            Console.WriteLine("Number of elements in queue3:" + queue3.Count());  
        }
    }
}

The output looks like this,

pic1.jpg
 

Queue.Enqueue Method

 
The Queue.Enqueue method adds an object to the end of the Queue<T>. For example, 
namespace Queue {  
    classProgram {  
        staticvoid Main(string[] args) {  
            Queue < string > queue1 = newQueue < string > ();  
            queue1.Enqueue("MCA");  
            queue1.Enqueue("MBA");  
            queue1.Enqueue("BCA");  
            queue1.Enqueue("BBA");  
            Console.WriteLine("The elements in the queue are:");  
            foreach(string s in queue1) {  
                Console.WriteLine(s);  
            }  
        }  
    }  
} 

OUTPUT 

pic2.jpg
 

Queue.Dequeue() method

 
Removes and returns the object at the beginning of the Queue<T>. For example, 
namespace Queue {  
    classProgram {  
        staticvoid Main(string[] args) {  
            Queue < string > queue1 = newQueue < string > ();  
            queue1.Enqueue("MCA");  
            queue1.Enqueue("MBA");  
            queue1.Enqueue("BCA");  
            queue1.Enqueue("BBA");  
            Console.WriteLine("The elements in the queue are:");  
            foreach(string s in queue1) {  
                Console.WriteLine(s);  
            }  
            queue1.Dequeue(); //Removes the first element that enter in the queue here the first element is MCA  
            queue1.Dequeue(); //Removes MBA  
            Console.WriteLine("After removal the elements in the queue are:");  
            foreach(string s in queue1) {  
                Console.WriteLine(s);  
            }  
        }  
    }  
}  

OUTPUT

pic3.jpg

Queue.Contain() method

Determines whether an element is in the Queue<T>. For example,

namespace Queue {  
    classProgram {  
        staticvoid Main(string[] args) {  
            Queue < string > queue1 = newQueue < string > ();  
            queue1.Enqueue("MCA");  
            queue1.Enqueue("MBA");  
            queue1.Enqueue("BCA");  
            queue1.Enqueue("BBA");  
            Console.WriteLine("The elements in the queue are:");  
            foreach(string s in queue1) {  
                Console.WriteLine(s);  
            }  
            Console.WriteLine("The element MCA is contain in the queue:" + queue1.Contains("MCA"));  
            Console.WriteLine("The element BCA is contain in the queue:" + queue1.Contains("BCA"));  
            Console.WriteLine("The element MTech is contain in the queue:" + queue1.Contains("MTech"));  
        }  
    }  
} 

OUTPUT

pic4.jpg

Queue.Clear() method

Removes all objects from theQueue<T>. For example,

namespace Queue {  
    classProgram {  
        staticvoid Main(string[] args) {  
            Queue < string > queue1 = newQueue < string > ();  
            queue1.Enqueue("MCA");  
            queue1.Enqueue("MBA");  
            queue1.Enqueue("BCA");  
            queue1.Enqueue("BBA");  
            Console.WriteLine("The elements in the queue are:" + queue1.Count());  
            queue1.Clear();  
            Console.WriteLine("The elements in the queue are after the clear method:" + queue1.Count());  
        }  
    }  
} 

OUTPUT

pic5.jpg
 

Queue.Peek method

 
Returns the object at the beginning of the Queue<T> without removing it. For example,
namespace Queue {  
    classProgram {  
        staticvoid Main(string[] args) {  
            Queue < string > queue1 = newQueue < string > ();  
            queue1.Enqueue("MCA");  
            queue1.Enqueue("MBA");  
            queue1.Enqueue("BCA");  
            queue1.Enqueue("BBA");  
            Console.WriteLine("Peek the first item from the queue is:" + queue1.Peek());  
            queue1.Dequeue();  
            Console.WriteLine("Peek the next item from the queue is:" + queue1.Peek());  
        }  
    }  
} 

OUTPUT

pic6.jpg
 

Queue.ToArray() method

 
Copies the Queue<T> elements to a new array. For example,
namespace Queue {  
    classProgram {  
        staticvoid Main(string[] args) {  
            Queue < string > queue1 = newQueue < string > ();  
            queue1.Enqueue("MCA");  
            queue1.Enqueue("MBA");  
            queue1.Enqueue("BCA");  
            queue1.Enqueue("BBA");  
            Console.WriteLine("The queue elements are:");  
            foreach(string s in queue1) {  
                Console.WriteLine(s);  
            }  
            Queue < string > queue2 = newQueue < string > (queue1.ToArray());  
            Console.WriteLine("\nContents of the copy");  
            foreach(string n in queue2) {  
                Console.WriteLine(n);  
            }  
        }  
    }  
} 

OUTPUT

pic7.jpg 
 

Summary

 
In this article I explained the Queue<> class and the various methods and properties of it.
 
Here are recommended articles on collections,
  1. C# Dictionary
  2. C# Queue
  3. C# Stack
  4. C# List
  5. C# Arrays
  6. C# HashTable
  7. C# StringCollection
  8. C# ArrayList
  9. C# HashSet
  10. C# LinkedList


Similar Articles