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

Definition

Bestimmt, ob die SortedList<TKey,TValue> einen bestimmten Schlüssel enthält.

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

Parameter

key
TKey

Der Schlüssel, der in der SortedList<TKey,TValue>Datei gefunden werden soll.

Gibt zurück

true wenn das SortedList<TKey,TValue> Element mit dem angegebenen Schlüssel enthält; andernfalls false.

Implementiert

Ausnahmen

key ist null.

Beispiele

Im folgenden Codebeispiel wird gezeigt, wie Sie die ContainsKey Methode verwenden, um zu testen, ob ein Schlüssel vorhanden ist, bevor die Add Methode aufgerufen wird. Außerdem wird gezeigt, wie TryGetValue die Methode zum Abrufen von Werten verwendet wird, was eine effiziente Möglichkeit zum Abrufen von Werten ist, wenn ein Programm häufig Schlüssel versucht, die sich nicht in der sortierten Liste befinden. Schließlich wird anhand der Item[] Eigenschaft (der Indexer in C#) die am wenigsten effiziente Methode zum Testen, ob Schlüssel vorhanden sind, gezeigt.

Dieses Codebeispiel ist Teil eines größeren Beispiels, das für die SortedList<TKey,TValue> Klasse bereitgestellt wird.

// 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", 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 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."

Hinweise

Bei dieser Methode handelt es sich um einen O(Log)-Vorgang. Dabei n handelt es sich um neinen O(LogCount)-Vorgang.

Gilt für:

Weitere Informationen