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 プロパティを使用して null 値を返すことができます。 Contains コレクションに特定のキーが存在するかどうかを判断するには、OrderedDictionary メソッドを使用します。

このメソッドは、EqualsでコレクションのオブジェクトのCompareToメソッドとitem メソッドを使用して、itemが存在するかどうかを判断します。

適用対象