Dictionary<TKey,TValue>.ContainsKey(TKey) Yöntem

Tanım

öğesinin Dictionary<TKey,TValue> belirtilen anahtarı içerip içermediğini belirler.

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

Parametreler

key
TKey

içinde Dictionary<TKey,TValue>bulunacak anahtar.

Döndürülenler

true belirtilen Dictionary<TKey,TValue> anahtara sahip bir öğe içeriyorsa; değilse, false.

Uygulamalar

Özel durumlar

key, null'e eşittir.

Örnekler

Aşağıdaki kod örneği, yöntemini çağırmadan önce bir anahtarın ContainsKey mevcut olup olmadığını test etmek için yönteminin Add nasıl kullanılacağını gösterir. Ayrıca, bir program sıklıkla sözlükte TryGetValue olmayan anahtarları denediğinde değerleri almak için etkili bir yol olan değerleri almak için yönteminin nasıl kullanılacağını gösterir. Son olarak, özelliğini (C# içindeki dizin oluşturucu) kullanarak anahtarların Item[] var olup olmadığını test etmenin en az verimli yolunu gösterir.

Bu kod örneği, sınıfı için Dictionary<TKey,TValue> sağlanan daha büyük bir örneğin parçasıdır (openWith bu örnekte kullanılan Sözlüğün adıdır).

// 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" |> not then
    openWith.Add("ht", "hypertrm.exe")
    printfn $"""Value added for key = "ht": {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 dictionary, 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 dictionary, TryGetValue can be a more efficient
// way to retrieve values.
match openWith.TryGetValue "tif" with
| true, value -> printfn $"For key = \"tif\", value = {value}."
| _ -> printfn "Key = \"tif\" is not found."
' When a program often has to try keys that turn out not to
' be in the dictionary, 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 dictionary.
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 dictionary.
try
    printfn $"""For key = "tif", value = {openWith["tif"]}"""
with :? KeyNotFoundException ->
    printfn "Key = \"tif\" is not found."
' The default Item property throws an exception if the requested
' key is not in the dictionary.
Try
    Console.WriteLine("For key = ""tif"", value = {0}.", _
        openWith("tif"))
Catch 
    Console.WriteLine("Key = ""tif"" is not found.")
End Try

Açıklamalar

Bu yöntem bir O(1) işlemine yaklaşır.

Şunlara uygulanır

Ayrıca bkz.