Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Check if an element is in the Queue in C#
To check if an element exists in a Queue in C#, you can use the Contains() method. This method returns true if the specified element is found in the queue, and false otherwise. The Queue<T> class in C# provides this built-in functionality for easy element searching.
Syntax
Following is the syntax for using the Contains() method −
bool result = queue.Contains(element);
Parameters
element − The element to search for in the queue
Return Value
The method returns a bool value −
trueif the element is found in the queuefalseif the element is not found in the queue
Using Contains() with String Queue
Example
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
Queue<string> queue = new Queue<string>();
queue.Enqueue("Electronics");
queue.Enqueue("Accessories");
queue.Enqueue("Toys");
queue.Enqueue("Books");
queue.Enqueue("Furniture");
queue.Enqueue("Clothing");
queue.Enqueue("Footwear");
queue.Enqueue("Cookware");
queue.Enqueue("Pet Supplies");
Console.WriteLine("Elements in the Queue...");
foreach(var element in queue) {
Console.WriteLine(element);
}
Console.WriteLine("Does the queue contain 'Books'? = " + queue.Contains("Books"));
Console.WriteLine("Does the queue contain 'Sports'? = " + queue.Contains("Sports"));
}
}
The output of the above code is −
Elements in the Queue... Electronics Accessories Toys Books Furniture Clothing Footwear Cookware Pet Supplies Does the queue contain 'Books'? = True Does the queue contain 'Sports'? = False
Using Contains() with Integer Queue
Example
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
Queue<int> queue = new Queue<int>();
queue.Enqueue(100);
queue.Enqueue(200);
queue.Enqueue(300);
queue.Enqueue(400);
queue.Enqueue(500);
queue.Enqueue(600);
queue.Enqueue(700);
queue.Enqueue(800);
queue.Enqueue(1000);
Console.WriteLine("Elements in the Queue...");
foreach(var element in queue) {
Console.WriteLine(element);
}
Console.WriteLine("Does the queue contain 500? = " + queue.Contains(500));
Console.WriteLine("Does the queue contain 50? = " + queue.Contains(50));
}
}
The output of the above code is −
Elements in the Queue... 100 200 300 400 500 600 700 800 1000 Does the queue contain 500? = True Does the queue contain 50? = False
How It Works
The Contains() method performs a linear search through the queue, comparing each element with the specified value using the default equality comparer for the type. For reference types, it uses reference equality, and for value types, it uses value equality.
Conclusion
The Contains() method provides a simple way to check if an element exists in a C# Queue. It returns true if the element is found and false otherwise, making it easy to perform membership tests on queue collections.
