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

定义

确定是否 IDictionary<TKey,TValue> 包含带有指定键的元素。Determines whether the IDictionary<TKey,TValue> contains an element with the specified key.

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

参数

key
TKey TKey TKey TKey

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

返回

如果 true 包含具有键的元素,则为 IDictionary<TKey,TValue>;否则为 falsetrue if the IDictionary<TKey,TValue> contains an element with the 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, which can be a more efficient way to retrieve values if a program frequently tries key values that are not in the dictionary. 最后,它说明了如何使用Item[TKey]属性插入项(中C#的索引器)。Finally, it shows how to insert items using Item[TKey] property (the indexer in C#).

此代码是可以编译和执行的更大示例的一部分。This code is part of a larger example that can be compiled and executed. 请参阅 System.Collections.Generic.IDictionary<TKey,TValue>See System.Collections.Generic.IDictionary<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
// 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

注解

实现在确定对象的相等性方面可能有所不同;例如, List<T>类使用Comparer<T>.Default,而Dictionary<TKey,TValue>类允许用户指定IComparer<T>要用于比较键的实现。Implementations can vary in how they determine equality of objects; for example, the List<T> class uses Comparer<T>.Default, whereas the Dictionary<TKey,TValue> class allows the user to specify the IComparer<T> implementation to use for comparing keys.

实现在是否允许key null时可能有所不同。Implementations can vary in whether they allow key to be null.

适用于