public bool TryGetValue (
TKey key,
out TValue value
)
参数- key
要获取的值的键。
- value
当此方法返回值时,如果找到该键,便会返回与指定的键相关联的值;否则,则会返回 value 参数的类型默认值。该参数未经初始化即被传递。
返回值如果 Dictionary 包含具有指定键的元素,则为 true;否则为 false。
string value = "";
if (openWith.TryGetValue("tif", out value))
{
Console.WriteLine("For key = /"tif/", value = {0}.", value);
}
else
{
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.");
}
|