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
Queue.Contains() Method in C#
The Queue.Contains() method in C# is used to determine whether a specific element exists in the Queue. It returns true if the element is found, otherwise false. This method performs a linear search through the queue elements.
Syntax
Following is the syntax for the Queue<T>.Contains() method −
public bool Contains(T item);
Parameters
-
item − The object to locate in the Queue. The value can be null for reference types.
Return Value
Returns true if the item is found in the Queue; otherwise, false.
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("Gary");
queue.Enqueue("Jack");
queue.Enqueue("Ryan");
queue.Enqueue("Kevin");
queue.Enqueue("Mark");
Console.WriteLine("Queue elements: " + string.Join(", ", queue));
Console.WriteLine("Count of elements = " + queue.Count);
Console.WriteLine("Does the queue contain 'Jack'? = " + queue.Contains("Jack"));
Console.WriteLine("Does the queue contain 'Alice'? = " + queue.Contains("Alice"));
// Search is case-sensitive
Console.WriteLine("Does the queue contain 'jack'? = " + queue.Contains("jack"));
}
}
The output of the above code is −
Queue elements: Gary, Jack, Ryan, Kevin, Mark Count of elements = 5 Does the queue contain 'Jack'? = True Does the queue contain 'Alice'? = False Does the queue contain 'jack'? = 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);
Console.WriteLine("Queue elements: " + string.Join(", ", queue));
Console.WriteLine("Count of elements = " + queue.Count);
Console.WriteLine("Does the queue contain 300? = " + queue.Contains(300));
Console.WriteLine("Does the queue contain 500? = " + queue.Contains(500));
// Remove elements and check again
queue.Dequeue(); // Remove 100
Console.WriteLine("After dequeue, contains 100? = " + queue.Contains(100));
}
}
The output of the above code is −
Queue elements: 100, 200, 300, 400 Count of elements = 4 Does the queue contain 300? = True Does the queue contain 500? = False After dequeue, contains 100? = False
Key Points
-
The
Contains()method uses the default equality comparer for the type to determine equality. -
For reference types, it can accept
nullas a valid search parameter. -
The search is case-sensitive for string comparisons.
-
Time complexity is O(n) as it performs a linear search through all elements.
Conclusion
The Queue.Contains() method provides a simple way to check if an element exists in a queue. It performs a linear search and returns a boolean value indicating whether the specified element is found in the queue collection.
