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

定义

确定是否 Dictionary<TKey,TValue> 包含指定键。Determines whether the Dictionary<TKey,TValue> contains the specified 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

参数

key
TKey TKey TKey TKey

要在 Dictionary<TKey,TValue> 中定位的键。The key to locate in the Dictionary<TKey,TValue>.

返回

如果 true 包含具有指定键的元素,则为 Dictionary<TKey,TValue>;否则为 falsetrue if the Dictionary<TKey,TValue> contains an element with the specified key; otherwise, false.

实现

异常

示例

下面的代码示例演示如何使用ContainsKey方法来测试在Add调用方法之前是否存在某个键。The following code example shows how to use the ContainsKey method to test whether a key exists prior to calling the Add method. 它还演示了如何使用TryGetValue方法来检索值,这是在程序频繁尝试不在字典中的键时检索值的有效方法。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 dictionary. 最后,它通过使用Item[TKey]属性(中C#的索引器)来显示测试项是否存在的效率最低的方法。Finally, it shows the least efficient way to test whether keys exist, by using the Item[TKey] property (the indexer in C#).

此代码示例是为Dictionary<TKey,TValue>类提供的更大示例的一部分(openWith是此示例中使用的字典的名称)。This code example is part of a larger example provided for the Dictionary<TKey,TValue> class (openWith is the name of the Dictionary used in this example).

// 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 dictionary, 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 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.
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
{
    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 dictionary.
Try
    Console.WriteLine("For key = ""tif"", value = {0}.", _
        openWith("tif"))
Catch 
    Console.WriteLine("Key = ""tif"" is not found.")
End Try

注解

此方法使用 O (1)操作。This method approaches an O(1) operation.

适用于

另请参阅