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 集合中尋找的索引鍵。

傳回

如果 OrderedDictionary 集合包含具有指定之索引鍵的元素,則為 true,否則為 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 (!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,則使用 屬性可以傳回 Null 值。 Contains使用方法來判斷集合中OrderedDictionary是否有特定索引鍵。

從 .NET Framework 2.0 開始,這個方法會使用 集合的物件 EqualsCompareTo 方法item來判斷是否存在item。 在舊版的 .NET Framework 中,已使用 Equals 集合中 物件的 和 CompareTo 方法item,來判斷此判斷。

適用於