IDictionary.Contains(Object) IDictionary.Contains(Object) IDictionary.Contains(Object) IDictionary.Contains(Object) Method


确定 IDictionary 对象是否包含具有指定键的元素。Determines whether the IDictionary object contains an element with the specified key.

 bool Contains(System::Object ^ key);
public bool Contains (object key);
abstract member Contains : obj -> bool
Public Function Contains (key As Object) As Boolean


Object Object Object Object

要在 IDictionary 对象中查找的键。The key to locate in the IDictionary object.


如果 true 包含具有键的元素,则为 IDictionary;否则为 falsetrue if the IDictionary contains an element with the key; otherwise, false.



下面的代码示例演示如何实现Contains方法。The following code example demonstrates how to implement the Contains method. 此代码示例是为IDictionary类提供的更大示例的一部分。This code example is part of a larger example provided for the IDictionary class.

    virtual bool Contains(Object^ key)
        int index;
        return TryGetIndexOfKey(key, &index);
public bool Contains(object key)
   Int32 index;
   return TryGetIndexOfKey(key, out index);
Public Function Contains(ByVal key As Object) As Boolean Implements IDictionary.Contains
    Dim index As Integer
    Return TryGetIndexOfKey(key, index)
End Function


实现在是否允许键null时可能有所不同。Implementations can vary in whether they allow the key to be null.

从 .NET Framework 2.0 开始, 此方法使用Equals集合的对象的和CompareTo方法item来确定是否item存在。Starting with the .NET Framework 2.0, this method uses the collection's objects' Equals and CompareTo methods on item to determine whether item exists. 在 .NET Framework 的早期版本中, 此决定是通过对集合中的Equals对象CompareTo使用item参数的和方法进行的。In the earlier versions of the .NET Framework, this determination was made by using the Equals and CompareTo methods of the item parameter on the objects in the collection.