The contains method in C# Queue checks for the presence of a specific element within the Queue collection. It outputs a Boolean value (true/false) to indicate whether the element is found in the Queue. This function uses the default equality comparer of the element type for comparison. If the specified element is present in the Queue, it returns true; otherwise, it returns false. This feature aids in verifying the presence of an item in the Queue efficiently while maintaining the Queue's order and content intact.
Queues in C# adhere to a first-in, first-out (FIFO) principle where elements are appended at the rear and removed from the front. The Contains method proves handy in verifying the existence of an element within the Queue prior to proceeding with additional operations.
Syntax:
It has the following syntax:
public virtual bool Contains(object ob);
Here, "ob" refers to the item that needs to be placed in the queue, and this value could potentially be null.
Return Value:
If the item is present in the Queue, the function will return True; if not, it will return False.
Example 1:
Let's consider a C# code snippet to demonstrate the implementation of the Queue.contains method.
using System;
using System.Collections;
class QueueContains{
// Driver code
public static void Main()
{
// An empty queue creation
Queue myQue = new Queue();
// the elements are added to the queue
myQue.Enqueue(10);
myQue.Enqueue(20);
myQue.Enqueue(30);
myQue.Enqueue(40);
myQue.Enqueue(50);
myQue.Enqueue(60);
myQue.Enqueue(70);
myQue.Enqueue(80);
// Checking if an element exists in the queue.
//The method returns True
//if the element can be found in the Queue; otherwise, it returns False value.
Console.WriteLine(myQue.Contains(8));
}
}
Output:
Example 2:
Let's consider a C# code snippet to demonstrate the implementation of the Queue.contains method.
using System;
using System.Collections;
class QueueContains{
// Driver code
public static void Main()
{
// An empty queue creation
Queue myQue = new Queue();
//The elements are added to the queue
myQue.Enqueue("C#");
myQue.Enqueue("is");
myQue.Enqueue("one");
myQue.Enqueue("of");
myQue.Enqueue("the");
myQue.Enqueue("Programming");
myQue.Enqueue("language");
// Checking if an element exists in the queue.
//The method returns True
//if the element can be found in the Queue; otherwise, it returns false value.
Console.WriteLine(myQue.Contains("of"));
}
}
Output:
Advantages of Queue.Contains Method in C#
There are several advantages of the Queue.Contains method in C#. Some main advantages of this method are as follows:
- Presence Verification: It efficiently determines if a certain element exists in the Queue without changing its structure or contents. It is especially important when we need to know if an item is present before continuing with other tasks.
- Simple Implementation: The Contains method is simple and easy to use. It encapsulates the fundamental logic of searching for an element in the Queue, allowing developers to concentrate on application logic instead of implementation specifics.
- Generic Support: When utilizing generic Queues (Queue<T>) , the Contains function works perfectly with strongly typed components, ensuring type safety and preventing runtime issues caused by incorrect type handling.
- Ease of integration: It works well with conditional statements and processes for making decisions. It makes logical examinations for branching in code execution easier by delivering a Boolean result based on whether or not an element is detected.
- Flexible Equality Comparison: The function uses the default equality comparer for the element type, allowing developers to compare items using customized equality criteria if the Equals method is correctly configured for custom types.
- Time Complexity: Although it takes O(n) time to iterate through each element in the Queue, it is often efficient when dealing with small to moderate-sized Queues. For bigger Queues or more frequent queries, try improving the search process or using alternate data structures designed for quicker lookups.