Home  >  Article  >  Backend Development  >  Queue in C#

Queue in C#

WBOY
WBOYOriginal
2024-09-03 15:30:43744browse

A queue is a collection of object it represents in the form of FIFO (First-In-First-Out) order the element which is added first will come out first, in C# Queue collection class present in the namespace System.Collection. Queue stores the element in FIFO order in which we can retrieve in a first-in, first-out manner of accessing elements. A queue is exactly just opposite to Stack Collection, where Stack is LIFO (Last-In-First-Out). The collection of Queue allows numerous null and duplicate values. Queue uses two methods called Enqueue() and Dequeue() which used for adding and retrieving values respectively.

Syntax:

The queue is created using the data type called Queue. Here the “new” keyword is used for creating an object of the queue. In queue collection for adding an item, we using the Enqueue method and for deleting an item we using the Dequeue method.

Queue QueueObject = new Queue() // creation of Queue
QueueObject.Enqueue(element) // to add element to Queue
QueueObject.Dequeue() //to remove element to Queue

How Queue works in C#?

Queue present in the form of FIFO (First-In-First-Out) it’s a collection of objects, this process is used when we need to access in first-in, first-out access of items. The queue is a non-generic it uses the type of collection which is defined in the System.Collections namespace. In general, a queue is useful when we use the information in the way which we stored in the queue collection.

The Queue implements through the interfaces called IEnumerable, ICloneable, ICollection. For the reference types, it accepts the null valid values. In queue collection for adding an item, we using the Enqueue method and for deleting an item we using the Dequeue method when adding an item to queue the total capacity is automatically increase for required internal memory.

Example:

using System;
using System.Collections;
public class QueueProgram {
static public void Main()
{
// to create a queue - using Queue class
Queue _objQueue = new Queue();
// to add an elements in Queue - using Enqueue() method
_objQueue.Enqueue("DotNet");
_objQueue.Enqueue("SQL");
_objQueue.Enqueue("Java");
_objQueue.Enqueue("PHP");
_objQueue.Enqueue("Android");
Console.WriteLine("Working Process of Queue\n");
Console.WriteLine("Number of Elements Present in Object(_objQueue)   : {0}", _objQueue.Count);
// to obtain the topmost element of _objQueue - using Dequeue method
Console.WriteLine("\nTo Get the topmost element in Queue" + " is            : {0}", _objQueue.Dequeue());
Console.WriteLine("\nNumber of Elements Present in Object(_objQueue)   : {0}", _objQueue.Count);
// to obtain the topmost element of _objQueue - using Peek method
Console.WriteLine("\nTo Get the topmost element in Queue is            : {0}", _objQueue.Peek());
Console.WriteLine("\nNumber of Elements Present in Object(_objQueue)   : {0}", _objQueue.Count);
// to check hether the element is present in the Queue
if (_objQueue.Contains("SQL") == true)
{
Console.WriteLine("\nElement is Present !");
}
else
{
Console.WriteLine("\nElement is not Present !");
}
}
}

In the above program, we declare the Queue as _ objQueue to hold the items of Queue. For adding the new element we using the method Enqueue() and for deleting the element we using the Dequeue() method. The property Count is used to get the total number of elements in the queue, the return value of this property is a number. Another method Contains() is used to check whether the given value/element is present, it returns the bool value either true or false. The Peek() is used to obtain the topmost value in the queue collection.

Output:

Queue in C#

From the above output, it shows that the items of the Queue are displayed. Firstly it displays the total number of elements present in the queue by using the Count() method and then it displays the topmost element by using the Peek() method. By using the Contains() method it checks whether the element present in the queue collection.

Constructors

In Queue class it consists of constructors that are used to create a queue.

  • Queue(): The constructor Queue() is used for creating the instance of queue class, it helps in the use of default growth factor.
  • Queue(ICollection): This constructor is used for creating an instance of the queue and it contains the items copied from the specified collection and having the same capacity as the number of items copied. It also uses the default initial growth factor.
  • Queue(Int32): This constructor is used to create a Queue class instance that is empty and has initial capacity specified, and uses the default growth factor.
  • Queue(Int32, Single): This constructor is used to create a Queue class instance that is empty and has initial capacity specified, and uses the default growth factor.

Queue functions in C# method

Let’s see the following functions list which is commonly used methods of the Queue class −

  • Enqueue(): Enqueue method is used when adding an element in Queue, it is a non-generic collection so we can add an element of any datatype in this method. The signature used for this method is void Enqueue(object obj)
  • Dequeue(): Dequeue method is for access queue which is used to retrieve the topmost element in the queue. By the FIFO approach Dequeue used to remove and its resultant one which returns the first element in the queue collection, the Dequeue() is called only when the total count of the queue is always greater than zero otherwise it throws an exception. The signature used for this method is object Dequeue()
  • Peek(): This method will return always the first element from the queue collection without removing from the queue. It throws an exception if the empty queue collection is called.
  • The signature used for this method is object Peek().
  • Clear(): This method is used to remove objects from the queue collection. The signature used for this method is void Clear().
  • Contains(): This method is used to check whether an element exists in the collection of Queue. The signature used for this method is bool Contains(object obj).
  • Clone(): Clone() method is used for creating a shallow copy of queue collection.
  • Equals(Object): This method is used to check whether the particular object is equal to the current object.
  • Synchronized(Queue): This method returns a new queue which encloses the original queue.
  • TrimToSize(): This method is used to set the capacity to which the actual number of items in the queue collection.

Conclusion

In this article, we came to know the Queue() usage in C#, it is based on the FIFO concept, for adding and deleting the queue we using the Enqueue() and Dequeue() methods respectively.

The above is the detailed content of Queue in C#. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:C# Using StaticNext article:C# Using Static