List<T>::Contains Method (T)

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Determines whether an element is in the List<T>.

Namespace:   System.Collections.Generic
Assembly:  mscorlib (in mscorlib.dll)

public:
virtual bool Contains(
	T item
) sealed

Parameters

item
Type: T

The object to locate in the List<T>. The value can be null for reference types.

Return Value

Type: System::Boolean

true if item is found in the List<T>; otherwise, false.

This method determines equality by using the default equality comparer, as defined by the object's implementation of the IEquatable<T>::Equals method for T (the type of values in the list).

This method performs a linear search; therefore, this method is an O(n) operation, where n is Count.

The following example demonstrates the Contains and Exists methods on a List<T> that contains a simple business object that implements Equals.

No code example is currently available or this language may not be supported.

The following example contains a list of complex objects of type Cube. The Cube class implements the IEquatable<T>::Equals method so that two cubes are considered equal if their dimensions are the same. In this example, the Contains method returns true, because a cube that has the specified dimensions is already in the collection.

No code example is currently available or this language may not be supported.

Universal Windows Platform
Available since 8
.NET Framework
Available since 2.0
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
Show: