Table of Contents
How Queue works in C#?
Constructors
Queue functions in C# method
Conclusion

Queue in C#

Sep 03, 2024 pm 03:30 PM
c# c# tutorial

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
Copy after login
QueueObject.Enqueue(element) // to add element to Queue
Copy after login
QueueObject.Dequeue() //to remove element to Queue
Copy after login

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 !");
}
}
}
Copy after login

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1267
29
C# Tutorial
1239
24
Active Directory with C# Active Directory with C# Sep 03, 2024 pm 03:33 PM

Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

Random Number Generator in C# Random Number Generator in C# Sep 03, 2024 pm 03:34 PM

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

C# Data Grid View C# Data Grid View Sep 03, 2024 pm 03:32 PM

Guide to C# Data Grid View. Here we discuss the examples of how a data grid view can be loaded and exported from the SQL database or an excel file.

Factorial in C# Factorial in C# Sep 03, 2024 pm 03:34 PM

Guide to Factorial in C#. Here we discuss the introduction to factorial in c# along with different examples and code implementation.

The difference between multithreading and asynchronous c# The difference between multithreading and asynchronous c# Apr 03, 2025 pm 02:57 PM

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.

Patterns in C# Patterns in C# Sep 03, 2024 pm 03:33 PM

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

Prime Numbers in C# Prime Numbers in C# Sep 03, 2024 pm 03:35 PM

Guide to Prime Numbers in C#. Here we discuss the introduction and examples of prime numbers in c# along with code implementation.

C# vs. C  : History, Evolution, and Future Prospects C# vs. C : History, Evolution, and Future Prospects Apr 19, 2025 am 12:07 AM

The history and evolution of C# and C are unique, and the future prospects are also different. 1.C was invented by BjarneStroustrup in 1983 to introduce object-oriented programming into the C language. Its evolution process includes multiple standardizations, such as C 11 introducing auto keywords and lambda expressions, C 20 introducing concepts and coroutines, and will focus on performance and system-level programming in the future. 2.C# was released by Microsoft in 2000. Combining the advantages of C and Java, its evolution focuses on simplicity and productivity. For example, C#2.0 introduced generics and C#5.0 introduced asynchronous programming, which will focus on developers' productivity and cloud computing in the future.

See all articles