OrderedDictionary.Contains(Object) Méthode

Définition

Détermine si la collection OrderedDictionary contient une clé spécifique.

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

Paramètres

key
Object

Clé à rechercher dans la collection OrderedDictionary.

Retours

true si la collection OrderedDictionary contient un élément avec la clé spécifiée ; sinon, false.

Implémente

Exemples

L’exemple de code suivant illustre la modification d’une OrderedDictionary collection. Dans cet exemple, la Contains méthode est utilisée pour déterminer si une entrée existe avant de tenter de la supprimer. Ce code fait partie d’un exemple de code plus large qui peut être consulté à l’adresse 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

Remarques

L’utilisation de la Item[] propriété peut retourner une valeur Null si la clé n’existe pas ou si la clé est null. Utilisez la Contains méthode pour déterminer si une clé spécifique existe dans la OrderedDictionary collection.

À compter de .NET Framework 2.0, cette méthode utilise les objets et CompareTo les Equals méthodes de la collection pour item déterminer s’il item existe. Dans les versions antérieures du .NET Framework, cette détermination était effectuée à l’aide Equals des méthodes et CompareTo du item paramètre sur les objets de la collection.

S’applique à