C# List Contains() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
List

Definition

In C#, List.Contains(item) answers a yes-or-no question: is this value somewhere in the list? It returns true if it finds an equal item, and false if it does not.

The method walks through the list from the first item until it finds a match, so on a big list it takes longer than a lookup in a HashSet. If you also need the position of the match, use IndexOf(). If you need to test with a rule instead of equality, use Exists().

This is an instance member of List<T> (System.Collections.Generic). Call it on a list variable, such as numbers.Contains(...). A List<T> is a class, so two variables can share one list. It keeps items in the order you put them, allows duplicates, and grows when needed. An array, by comparison, has a fixed length.

Syntax

Pass the value to look for. The result is a bool, so it fits directly into an if.

C#
List<string> names = new List<string> { "Asha", "Ben" };
bool a = names.Contains("Ben");    // true
bool b = names.Contains("ben");    // false: letter case matters
CallWhat it does
names.Contains("Ben")true if the list holds "Ben" exactly.
numbers.Contains(99)false when no item equals 99.
if (!names.Contains(x)) names.Add(x);A common pattern: add only when the value is not there yet.

Contains uses the default equality check, which calls Equals. For numbers and strings that compares the values. For your own classes it compares object references, unless the class overrides Equals. It cannot take a comparer; use LINQ's names.Contains("x", StringComparer.OrdinalIgnoreCase) for that.

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

Parameters

ParameterDescription
itemRequired. The value to look for. A null is allowed for reference types, and it matches a null item.

Return Value

Return ValueDescription
booltrue if an equal item is in the list. false otherwise, including for an empty list.

C# Examples

Example 1 — Is It in the List?

Check for a number that is present and one that is missing, then check a string with different letter case.

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

class Program {
    static void Main() {
        List<int> numbers = new List<int> { 10, 20, 30 };
        Console.WriteLine(numbers.Contains(20));
        Console.WriteLine(numbers.Contains(99));

        List<string> names = new List<string> { "Asha", "Ben" };
        Console.WriteLine(names.Contains("Ben"));
        Console.WriteLine(names.Contains("ben"));
    }
}

How It Works

  1. Present. 20 is the second item, so the scan stops there and returns True.
  2. Missing. No item equals 99. The scan reaches the end and returns False, without an exception.
  3. Exact string. "Ben" matches the stored text letter for letter.
  4. Case-sensitive. "ben" is a different string from "Ben", so the last line is False.

Example 2 — Contains with Your Own Classes

Two classes hold the same kind of data. Only the one that overrides Equals can be found with a look-alike object.

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

class Plain {
    public int Value;
}

class Coin {
    public int Value;
    public override bool Equals(object obj) {
        return obj is Coin other && other.Value == Value;
    }
    public override int GetHashCode() { return Value; }
}

class Program {
    static void Main() {
        List<Plain> plains = new List<Plain> { new Plain { Value = 5 } };
        List<Coin> coins = new List<Coin> { new Coin { Value = 5 } };

        Console.WriteLine(plains.Contains(new Plain { Value = 5 }));
        Console.WriteLine(coins.Contains(new Coin { Value = 5 }));
    }
}

How It Works

  1. Default is by reference. Plain has no Equals of its own, so the list can only match the very same object, and a new one never matches.
  2. Look-alike fails. The new Plain holds 5 as well, but it is another object, so the first line is False.
  3. Override Equals. Coin says two coins are equal when their Value is the same. Contains calls that method for each item.
  4. GetHashCode too. When you override Equals, also override GetHashCode so hash-based types like HashSet behave in the same way.

Example 3 — Contains That Ignores Letter Case

List.Contains has no comparer parameter. Use the LINQ version, or Exists with your own test.

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

class Program {
    static void Main() {
        List<string> names = new List<string> { "Asha", "Ben", "Chen" };

        Console.WriteLine(names.Contains("CHEN"));
        Console.WriteLine(names.Contains("CHEN", StringComparer.OrdinalIgnoreCase));
        Console.WriteLine(names.Exists(n => n.Equals("ben", StringComparison.OrdinalIgnoreCase)));
    }
}

How It Works

  1. Plain call. The built-in List.Contains compares exactly, so "CHEN" does not match "Chen".
  2. LINQ overload. With using System.Linq;, the extension method Contains(value, comparer) becomes available on any list.
  3. The comparer decides. StringComparer.OrdinalIgnoreCase treats upper and lower case as the same letter, so the second line is True.
  4. Exists is another way. Exists takes a test lambda, so you can write any rule, here a case-insensitive Equals for "ben".

Frequently Asked Questions

It checks whether the list holds an item equal to the value you pass, and returns true or false.
Yes. "Ben" and "ben" are different. For a case-insensitive check use the LINQ overload Contains(value, StringComparer.OrdinalIgnoreCase) or Exists with your own test.
Contains compares with a value by equality. Exists takes a predicate (a test lambda), so it can check any rule such as "is there a number above 50?".
For classes, the default equality is by reference. Override Equals and GetHashCode in your class if two objects with the same data should count as equal.
It is O(n): it checks items one by one. For many look-ups on big data, a HashSet<T> or a Dictionary is faster.

Next: ConvertAll()

Create a new List by converting every item with a function (a Converter).

ConvertAll() 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.

12 people found this page helpful