語言

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[],使用該null屬性可返回空值。 使用此 Contains 方法判斷集合中 OrderedDictionary 是否存在特定金鑰。

此方法利用集合中的物件與 EqualsCompareTo 方法 item 來判斷是否 item 存在。

適用於