SortedList<TKey,TValue>.ContainsKey(TKey) SortedList<TKey,TValue>.ContainsKey(TKey) SortedList<TKey,TValue>.ContainsKey(TKey) SortedList<TKey,TValue>.ContainsKey(TKey) Method

Definición

Determina si SortedList<TKey,TValue> contiene una clave específica.Determines whether the SortedList<TKey,TValue> contains a specific key.

public:
 virtual bool ContainsKey(TKey key);
public bool ContainsKey (TKey key);
abstract member ContainsKey : 'Key -> bool
override this.ContainsKey : 'Key -> bool
Public Function ContainsKey (key As TKey) As Boolean

Parámetros

key
TKey TKey TKey TKey

Clave que se buscará en SortedList<TKey,TValue>.The key to locate in the SortedList<TKey,TValue>.

Devoluciones

true si la colección SortedList<TKey,TValue> contiene un elemento con la clave especificada; en caso contrario, false.true if the SortedList<TKey,TValue> contains an element with the specified key; otherwise, false.

Implementaciones

Excepciones

Ejemplos

En el ejemplo de código siguiente se muestra cómo ContainsKey utilizar el método para comprobar si existe una clave antes de Add llamar al método.The following code example shows how to use the ContainsKey method to test whether a key exists prior to calling the Add method. También se muestra cómo utilizar el TryGetValue método para recuperar valores, que es una manera eficaz de recuperar valores cuando un programa intenta con frecuencia claves que no están en la lista ordenada.It also shows how to use the TryGetValue method to retrieve values, which is an efficient way to retrieve values when a program frequently tries keys that are not in the sorted list. Por último, muestra la forma menos eficaz de comprobar si existen claves mediante la Item[TKey] propiedad (el indizador en C#).Finally, it shows the least efficient way to test whether keys exist, by using the Item[TKey] property (the indexer in C#).

Este ejemplo de código forma parte de un ejemplo más extenso proporcionado SortedList<TKey,TValue> para la clase.This code example is part of a larger example provided for the SortedList<TKey,TValue> class.

// ContainsKey can be used to test keys before inserting
// them.
if (!openWith->ContainsKey("ht"))
{
    openWith->Add("ht", "hypertrm.exe");
    Console::WriteLine("Value added for key = \"ht\": {0}",
        openWith["ht"]);
}
// ContainsKey can be used to test keys before inserting 
// them.
if (!openWith.ContainsKey("ht"))
{
    openWith.Add("ht", "hypertrm.exe");
    Console.WriteLine("Value added for key = \"ht\": {0}", 
        openWith["ht"]);
}
' ContainsKey can be used to test keys before inserting 
' them.
If Not openWith.ContainsKey("ht") Then
    openWith.Add("ht", "hypertrm.exe")
    Console.WriteLine("Value added for key = ""ht"": {0}", _
        openWith("ht"))
End If
// When a program often has to try keys that turn out not to
// be in the list, TryGetValue can be a more efficient
// way to retrieve values.
String^ value = "";
if (openWith->TryGetValue("tif", value))
{
    Console::WriteLine("For key = \"tif\", value = {0}.", value);
}
else
{
    Console::WriteLine("Key = \"tif\" is not found.");
}
// When a program often has to try keys that turn out not to
// be in the list, TryGetValue can be a more efficient 
// way to retrieve values.
string value = "";
if (openWith.TryGetValue("tif", out value))
{
    Console.WriteLine("For key = \"tif\", value = {0}.", value);
}
else
{
    Console.WriteLine("Key = \"tif\" is not found.");
}
' When a program often has to try keys that turn out not to
' be in the list, TryGetValue can be a more efficient 
' way to retrieve values.
Dim value As String = ""
If openWith.TryGetValue("tif", value) Then
    Console.WriteLine("For key = ""tif"", value = {0}.", value)
Else
    Console.WriteLine("Key = ""tif"" is not found.")
End If
// The indexer throws an exception if the requested key is
// not in the list.
try
{
    Console::WriteLine("For key = \"tif\", value = {0}.",
        openWith["tif"]);
}
catch (KeyNotFoundException^)
{
    Console::WriteLine("Key = \"tif\" is not found.");
}
// The indexer throws an exception if the requested key is
// not in the list.
try
{
    Console.WriteLine("For key = \"tif\", value = {0}.", 
        openWith["tif"]);
}
catch (KeyNotFoundException)
{
    Console.WriteLine("Key = \"tif\" is not found.");
}
' The default Item property throws an exception if the requested
' key is not in the list.
Try
    Console.WriteLine("For key = ""tif"", value = {0}.", _
        openWith("tif"))
Catch 
    Console.WriteLine("Key = ""tif"" is not found.")
End Try

Comentarios

Este método es una operación O ( nlog), donde n es Count.This method is an O(log n) operation, where n is Count.

Se aplica a

Consulte también: