C# List Contains Method

List type.

Contains scans a List. It searches for a specific element to see if that element occurs at least once in the collection. The C# List type has the useful Contains method that searches declaratively—without an explicit for-loop. This simplifies some programs.

This C# example program demonstrates the Contains method on the List type.

Example

Note

First, you will need to add the using System.Collections.Generic directive at the top of your source file to be using the List generic type. The Contains method is an instance method on the List type, which means you can only call it on a constructed List type. The List instance can have any type of element specified in its type parameter.

The Contains method accepts a single parameter indicating the element value you are searching for. The LINQ Contains method accepts two parameters, with an additional IEqualityComparer instance that specifies how elements are compared for equality.

Program that uses List Contains method [C#]

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
	// 1. Create List with three elements.
	var list = new List<string>();
	list.Add("cat");
	list.Add("dog");
	list.Add("moth");

	// 2. Search for this element.
	if (list.Contains("dog"))
	{
	    Console.WriteLine("dog was found");
	}

	// 3. Search for this element in any string case.
	//    This is the LINQ method with the same name.
	if (list.Contains("MOTH", StringComparer.OrdinalIgnoreCase))
	{
	    Console.WriteLine("MOTH was found (insensitive)");
	}

	// 4. This element is not found.
	Console.WriteLine(list.Contains("fish"));
    }
}

Output

dog was found                         [Part 2]
MOTH was found (insensitive)          [Part 3]
False                                 [Part 4]
Var keyword

Method invocations shown. First an implicitly typed local variable List is constructed with element type of string. Three string literal references are added to the collection's internal arrays through the Add method.

Next, various Contains methods are invoked three times. The first invocation shows only one parameter, and when the string "dog" is found, the text is written. The second method call uses two parameters, specifying the StringComparer.OrdinalIgnoreCase class that implements the IEqualityComparer interface. This allows an insensitive string search. Finally, an element that does not exist is searched for.

IEqualityComparer

Has Boolean result value. The Contains instance method on the List constructed type returns a bool value type. This means you can assign the result of Contains to a bool local variable, field, or property. You can also use the Contains method as an expression and pass it as a parameter to another method.

System.Linq Contains method

LINQ (language integrated query)

The System.Linq namespace, which is included by default in new Console applications in Visual Studio, will allow you to call a separate Contains generic method on IEnumerable types. The example in the program text shows the Contains extension method being used on the List type with a case-insensitive search. LINQ extension methods are often slower in execution than instance methods because they operate on the IEnumerable interface.

Implementation

.NET Framework information

The Contains method performs a linear search through the elements, starting with the first element, and uses the Equals method to check each element value. Naturally this is often much slower than a O(1) lookup collection such as a Dictionary. The LINQ method also does a linear search.

Summary

The C# programming language

We looked at how you can use the Contains instance method on the List generic type in the C# programming language, and how this method differs from the LINQ Contains method. We saw how the Contains method returns a Boolean result indicating the elements existence; how the LINQ Contains method can be used on a List; and how the methods are generally implemented with a forward linear search.

List Examples Collections
.NET