SortedList<TKey,TValue>.ContainsKey(TKey) Método
Definición
Importante
Parte de la información hace referencia a la versión preliminar del producto, que puede haberse modificado sustancialmente antes de lanzar la versión definitiva. Microsoft no otorga ninguna garantía, explícita o implícita, con respecto a la información proporcionada aquí.
Determina si SortedList<TKey,TValue> contiene una clave específica.
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
Clave que se buscará en SortedList<TKey,TValue>.
Devoluciones
true
si la colección SortedList<TKey,TValue> contiene un elemento con la clave especificada; en caso contrario, false
.
Implementaciones
Excepciones
key
es null
.
Ejemplos
En el ejemplo de código siguiente se muestra cómo usar el ContainsKey método para probar si existe una clave antes de llamar al Add método . También muestra cómo usar 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. Por último, muestra la manera menos eficaz de probar si existen claves mediante la Item[] propiedad (el indexador en C#).
Este ejemplo de código es parte de un ejemplo más grande proporcionado para la clase SortedList<TKey,TValue>.
// 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
// ContainsKey can be used to test keys before inserting
// them.
if not (openWith.ContainsKey("ht")) then
openWith.Add("ht", "hypertrm.exe");
printfn """Value added for key = "ht": {openWith["ht"]}"""
// 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
// 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.
match openWith.TryGetValue("tif") with
| true, value ->
printfn "For key = \"tif\", value = {value}."
| false, _ ->
printfn "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 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
// The indexer throws an exception if the requested key is
// not in the list.
try
printfn $"""For key = "tif", value = {openWith["tif"]}."""
with
| :? KeyNotFoundException ->
printfn "Key = \"tif\" is not found."
Comentarios
Este método es una operación de O(log n
), donde n
es Count.