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 進行。

適用於