Collection<T>.Contains(T) 方法

定义

确定某元素是否在 Collection<T> 中。Determines whether an element is in the Collection<T>.

public:
 virtual bool Contains(T item);
public bool Contains (T item);
abstract member Contains : 'T -> bool
override this.Contains : 'T -> bool
Public Function Contains (item As T) As Boolean

参数

item
T

要在 Collection<T> 中查找的对象。The object to locate in the Collection<T>. 对于引用类型,该值可以为 nullThe value can be null for reference types.

返回

如果在 true 中找到 item,则为 Collection<T>;否则为 falsetrue if item is found in the Collection<T>; otherwise, false.

实现

注解

如果 Collection<T> 对象是使用无参数构造函数创建的,则此方法将使用默认的相等比较器 EqualityComparer<T>.Default 来确定是否 T(列表中的值的类型)的相等性。If the Collection<T> object is created using the parameterless constructor, this method determines equality using the default equality comparer EqualityComparer<T>.Default for T, the type of values in the list. 如果类型 T 未实现 IEquatable<T> 接口,则使用 Equals 方法。If type T does not implement the IEquatable<T> interface, the Equals method is used.

如果通过将 IList<T> 对象传递到构造函数来创建集合对象,则 Contains 方法将由该 IList<T> 对象使用的 IEqualityComparer<T> 接口确定。If the Collection object is created by passing an IList<T> object to the constructor, the Contains method will be determined by the IEqualityComparer<T> interface used by that IList<T> object.

此方法执行线性搜索;因此,平均执行时间与 Count成正比。This method performs a linear search; therefore, the average execution time is proportional to Count. 也就是说,此方法是一个 O (n)操作,其中 nCount的。That is, this method is an O(n) operation, where n is Count.

适用于

另请参阅