Dictionary<TKey,TValue>.TryGetValue(TKey, TValue) 方法
定义
获取与指定键关联的值。Gets the value associated with the specified key.
public:
virtual bool TryGetValue(TKey key, [Runtime::InteropServices::Out] TValue % value);
public bool TryGetValue (TKey key, out TValue value);
abstract member TryGetValue : 'Key * 'Value -> bool
override this.TryGetValue : 'Key * 'Value -> bool
Public Function TryGetValue (key As TKey, ByRef value As TValue) As Boolean
参数
- key
- TKey
要获取的值的键。The key of the value to get.
- value
- TValue
当此方法返回值时,如果找到该键,便会返回与指定的键相关联的值;否则,则会返回 value 参数的类型默认值。When this method returns, contains the value associated with the specified key, if the key is found; otherwise, the default value for the type of the value parameter. 此参数未经初始化即被传递。This parameter is passed uninitialized.
返回
如果 true 包含具有指定键的元素,则为 Dictionary<TKey,TValue>;否则为 false。true if the Dictionary<TKey,TValue> contains an element with the specified key; otherwise, false.
实现
例外
key 为 null。key is null.
示例
该示例演示如何使用方法来 TryGetValue 更有效地检索程序中不在字典中的键的值。The example shows how to use the TryGetValue method as a more efficient way to retrieve values in a program that frequently tries keys that are not in the dictionary. 相反,此示例还演示了 Item[] c ) # 中的索引器 (属性如何在尝试检索不存在的键时引发异常。For contrast, the example also shows how the Item[] property (the indexer in C#) throws exceptions when attempting to retrieve nonexistent keys.
此代码示例是为类提供的更大示例的一部分 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).
// 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
注解
此方法结合了 ContainsKey 方法和属性的功能 Item[] 。This method combines the functionality of the ContainsKey method and the Item[] property.
如果未找到键,则 value 参数将为类型 TValue 获取适当的默认值;例如,为整数类型获取 0(零),为布尔值类型获取 false,为引用类型获取 null。If the key is not found, then the value parameter gets the appropriate default value for the type TValue; for example, 0 (zero) for integer types, false for Boolean types, and null for reference types.
TryGetValue如果代码频繁尝试访问不在字典中的键,请使用方法。Use the TryGetValue method if your code frequently attempts to access keys that are not in the dictionary. 使用此方法比捕获属性引发的更有效 KeyNotFoundException Item[] 。Using this method is more efficient than catching the KeyNotFoundException thrown by the Item[] property.
此方法使用 O (1) 操作。This method approaches an O(1) operation.