C# Queue Contains() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Queue

Definition

In C#, Queue.Contains(item) checks whether an item is anywhere in a Queue<T>. It returns true if the item is found and false if not. The queue is not changed.

Think of asking, "Is Ben somewhere in this line?" The clerk has to look at the line person by person from the front. A queue has no fast lookup, so Contains is a linear scan, O(n). If you do this check very often on a big collection, a HashSet<T> is a better helper alongside the queue.

This is an instance member of Queue<T> (System.Collections.Generic). Call it on a queue variable, such as line.Contains(...). A Queue<T> is a class, so two variables can share one queue. It is first in, first out (FIFO): items leave in the order they arrived. It is not thread-safe; for several threads use ConcurrentQueue<T>.

Syntax

Pass the item you want to find. It returns a bool.

C#
Queue<string> line = new Queue<string>();
line.Enqueue("Asha");
line.Enqueue("Ben");

bool a = line.Contains("Ben");   // true
bool b = line.Contains("Zoe");   // false
CallWhat it does
line.Contains("Ben")true if some item in the queue equals "Ben".
line.Contains("ben")false if the queue holds "Ben". String comparison here is case-sensitive.
line.Contains(null)true only if a null was enqueued. It does not throw.
line.Peek()If you only care about the front item, Peek() is faster than Contains.

Contains compares with EqualityComparer<T>.Default. For string and numbers that means normal value equality. For your own classes it means reference equality (the same object) unless the class overrides Equals. There is no overload that accepts a custom comparer, so for case-insensitive search, convert your items or use LINQ.

Library declarations
C#
public bool Contains(T item);

Parameters

ParameterDescription
itemThe item to look for. It may be null for reference types.

Return Value

Return ValueDescription
booltrue if the item is in the queue. false if it is not, or if the queue is empty.

C# Examples

Example 1 — Check Who Is in the Line

Search for a name that is waiting and one that is not. Then check that searching did not change the queue.

C#
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        Queue<string> line = new Queue<string>();
        line.Enqueue("Asha");
        line.Enqueue("Ben");
        line.Enqueue("Chitra");

        Console.WriteLine(line.Contains("Ben"));
        Console.WriteLine(line.Contains("Zoe"));
        Console.WriteLine(line.Count);
        Console.WriteLine(line.Peek());
    }
}

How It Works

  1. Found anywhere. Ben is in the middle of the line, and Contains still finds him. Position does not matter.
  2. Not found. No one called Zoe is waiting, so the answer is False.
  3. Search is read-only. Count is still 3 after two searches.
  4. Front is untouched. Peek() still shows Asha, so the order was not disturbed either.

Example 2 — Case Matters, and null Can Be Found

Strings are compared exactly, including upper and lower case. A queue can also hold null, and Contains can find it.

C#
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        Queue<string> names = new Queue<string>();
        names.Enqueue("Asha");

        Console.WriteLine(names.Contains("Asha"));
        Console.WriteLine(names.Contains("asha"));
        Console.WriteLine(names.Contains(null));

        names.Enqueue(null);
        Console.WriteLine(names.Contains(null));
        Console.WriteLine(names.Count);
    }
}

How It Works

  1. Exact match. "Asha" is stored, so the first search is True.
  2. Case-sensitive. The lowercase "asha" is a different string, so the second search prints False.
  3. No null yet. Asking for null before any null was added gives False, and no exception is thrown.
  4. Null is stored like any item. After Enqueue(null), the search says True and Count is 2.

Example 3 — Enqueue Only If Not Already Waiting

Use Contains as a gatekeeper so the same request number is queued only once.

C#
using System;
using System.Collections.Generic;

class Program {
    static void Enqueue(Queue<int> queue, int id) {
        if (!queue.Contains(id)) {
            queue.Enqueue(id);
        }
    }

    static void Main() {
        Queue<int> requests = new Queue<int>();

        Enqueue(requests, 5);
        Enqueue(requests, 8);
        Enqueue(requests, 5);
        Enqueue(requests, 8);
        Enqueue(requests, 9);

        Console.WriteLine(requests.Count);
        Console.WriteLine(string.Join(", ", requests));
    }
}

How It Works

  1. A small helper. The helper method adds an id only when Contains says it is missing.
  2. Repeats are skipped. The second 5 and the second 8 are already waiting, so their Enqueue calls are never made.
  3. Arrival order is kept. The three ids that got in are printed front to back: 5, 8, 9.
  4. Trade-off. Each check scans the queue, so this is fine for short queues. For thousands of items, keep a separate HashSet of ids for the check.

Frequently Asked Questions

It returns true if the item is anywhere in the queue, and false if it is not. It does not change the queue.
It is O(n). It checks the items one by one from the front until it finds a match, so a long queue takes longer to search.
Yes. Contains("Ben") does not match "ben". Convert both sides to the same case, or use LINQ with a comparer, for a case-insensitive search.
It uses the default equality. Without an Equals override, it only matches the very same object. Override Equals and GetHashCode in your class if you want matching by value.
Yes, if you enqueued a null earlier. The call itself never throws for null.

Next: CopyTo()

Copy the queue's items into an existing array, starting at a position you choose.

CopyTo() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful