Queue.Enqueue() Method in C#The Queue.Enqueue() method is used to add an item to the end of the Queue. A queue is a data structure that operates on the FirstInFirstOut (FIFO) principle, which states that the element inserted first is the first to be withdrawn. The Enqueue() function belongs to the Queue<T> class, where T denotes the type of entries in the queue. It comes under the System.Collections package. This approach is an O(1) operation if the Count is smaller than the capacity of the inner array. If the internal array must be reallocated to accommodate the additional element, this approach becomes an O(n) operation, in which n is Count. When you call the Enqueue() function, you pass an argument corresponding to the element you would like to add to the queue. After that, this element is added to the end of the queue, so it is the last element in the line. If the list of elements is empty, the new element becomes the queue's single element. The time complexity of the Enqueue() function is O(1), which indicates that it has a constant time complexity irrespective of the size of the queue. New components may be added to a queue without requiring the old elements to be traversed or rearranged. Syntax:It has the following syntax: Here, the ob indicates the object that is to be added. Example:Let us take an example to illustrate the Queue.Enqueue() method in C#. Output: The number of elements in the queue are:1 The number of elements in the queue are:2 The number of elements in the queue are:3 The number of elements in the queue are:4 The number of elements in the queue are:5 The number of elements in the queue are:6 Explanation:
Advantages of Queue.Enqueue() Method in C#There are several advantages of the Queue.Enqueue() method in C#. Some main advantages of this method are as follows:
Because of their FIFO nature, queues are adaptable and may be utilized in several algorithms and data structures (including graphs, trees, and others). As a result, they are a key building component for many other complicated data structures and algorithms.
Next TopicSingle.IsInfinity() Method in C#
