OrderedDictionary.Contains(Object) Método
Definição
Importante
Algumas informações se referem a produtos de pré-lançamento que podem ser substancialmente modificados antes do lançamento. A Microsoft não oferece garantias, expressas ou implícitas, das informações aqui fornecidas.
Determina se a OrderedDictionary coleção contém uma chave específica.
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
Parâmetros
- key
- Object
A chave a ser localizada na OrderedDictionary coleção.
Retornos
true se a OrderedDictionary coleção contiver um elemento com a chave especificada; caso contrário, false.
Implementações
Exemplos
O exemplo de código a seguir demonstra a modificação de uma OrderedDictionary coleção. Neste exemplo, o Contains método é usado para determinar se existe uma entrada antes de tentar removê-la. Esse código faz parte de um exemplo de código maior que pode ser exibido em 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
Comentários
Usar a Item[] propriedade pode retornar um valor nulo se a chave não existir ou se a chave for null. Use o Contains método para determinar se existe uma chave específica na OrderedDictionary coleção.
Esse método usa os objetos e CompareTo os métodos Equalsitem da coleção para determinar se item existe.