OrderedDictionary.Contains(Object) 方法

定义

确定集合是否 OrderedDictionary 包含特定键。

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

参数

key
Object

在集合中查找的 OrderedDictionary 键。

返回

true 如果集合包含具有指定键的元素,则为 OrderedDictionary ;否则为 false

实现

示例

下面的代码示例演示集合的 OrderedDictionary 修改。 在此示例中, Contains 该方法用于确定条目是否存在,然后再尝试删除它。 此代码是可在以下位置 OrderedDictionary查看的较大代码示例的一部分。

// Modifying the OrderedDictionary
if (!myOrderedDictionary.IsReadOnly)
{
    // Insert a new key to the beginning of the OrderedDictionary
    myOrderedDictionary.Insert(0, "insertedKey1", "insertedValue1");

    // Modify the value of the entry with the key "testKey2"
    myOrderedDictionary["testKey2"] = "modifiedValue";

    // Remove the last entry from the OrderedDictionary: "testKey3"
    myOrderedDictionary.RemoveAt(myOrderedDictionary.Count - 1);

    // Remove the "keyToDelete" entry, if it exists
    if (myOrderedDictionary.Contains("keyToDelete"))
    {
        myOrderedDictionary.Remove("keyToDelete");
    }
}
' Modifying the OrderedDictionary
If Not myOrderedDictionary.IsReadOnly Then

    ' Insert a new key to the beginning of the OrderedDictionary
    myOrderedDictionary.Insert(0, "insertedKey1", "insertedValue1")

    ' Modify the value of the entry with the key "testKey2"
    myOrderedDictionary("testKey2") = "modifiedValue"

    ' Remove the last entry from the OrderedDictionary: "testKey3"
    myOrderedDictionary.RemoveAt(myOrderedDictionary.Count - 1)

    ' Remove the "keyToDelete" entry, if it exists
    If (myOrderedDictionary.Contains("keyToDelete")) Then
        myOrderedDictionary.Remove("keyToDelete")
    End If
End If

注解

Item[]如果该键不存在或键为 nullnull,则使用该属性可以返回 null 值。 Contains使用该方法确定集合中OrderedDictionary是否存在特定键。

此方法使用集合的对象 EqualsCompareTo 方法 item 来确定是否存在 item

适用于