Dictionary.TryGetValue 方法

论坛 期权论坛 脚本     
匿名网站用户   2020-12-19 18:07   33   0
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.");
        }

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:1136255
帖子:227251
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP