从MSDN的词条词典。TryGetValue方法:

This method combines the functionality of the ContainsKey method and the Item property. If the key is not found, then the value parameter gets the appropriate default value for the value type TValue; for example, 0 (zero) for integer types, false for Boolean types, and null for reference types. Use the TryGetValue method if your code frequently attempts to access keys that are not in the dictionary. Using this method is more efficient than catching the KeyNotFoundException thrown by the Item property. This method approaches an O(1) operation.

从描述中,不清楚它是否比调用ContainsKey然后进行查找更高效,还是更方便。TryGetValue的实现只是调用ContainsKey,然后Item,还是实际上比做单个查找更有效?

换句话说,哪个更有效(即哪个查找更少):

Dictionary<int,int> dict;
//...//
int ival;
if(dict.ContainsKey(ikey))
{
  ival = dict[ikey];
}
else
{
  ival = default(int);
}

or

Dictionary<int,int> dict;
//...//
int ival;
dict.TryGetValue(ikey, out ival);

注意:我不是在寻找一个基准!


当前回答

谁在乎呢? -)

您可能会问这个问题,因为TryGetValue使用起来很麻烦——所以用扩展方法像这样封装它。

public static class CollectionUtils
{
    // my original method
    // public static V GetValueOrDefault<K, V>(this Dictionary<K, V> dic, K key)
    // {
    //    V ret;
    //    bool found = dic.TryGetValue(key, out ret);
    //    if (found)
    //    {
    //        return ret;
    //    }
    //    return default(V);
    // }


    // EDIT: one of many possible improved versions
    public static TValue GetValueOrDefault<K, V>(this IDictionary<K, V> dictionary, K key)
    {
        // initialized to default value (such as 0 or null depending upon type of TValue)
        TValue value;  

        // attempt to get the value of the key from the dictionary
        dictionary.TryGetValue(key, out value);
        return value;
    }

然后只需呼叫:

dict.GetValueOrDefault("keyname")

or

(dict.GetValueOrDefault("keyname") ?? fallbackValue) 

其他回答

由于到目前为止没有一个答案真正回答了这个问题,下面是我在一些研究后找到的一个可以接受的答案:

如果你反编译TryGetValue,你会看到它是这样做的:

public bool TryGetValue(TKey key, out TValue value)
{
  int index = this.FindEntry(key);
  if (index >= 0)
  {
    value = this.entries[index].value;
    return true;
  }
  value = default(TValue);
  return false;
}

而ContainsKey方法是:

public bool ContainsKey(TKey key)
{
  return (this.FindEntry(key) >= 0);
}

所以TryGetValue只是ContainsKey加上数组查找,如果项目是存在的。

似乎TryGetValue的速度几乎是ContainsKey+Item组合的两倍。

一个快速的基准测试显示TryGetValue略有优势:

    static void Main() {
        var d = new Dictionary<string, string> {{"a", "b"}};
        var start = DateTime.Now;
        for (int i = 0; i != 10000000; i++) {
            string x;
            if (!d.TryGetValue("a", out x)) throw new ApplicationException("Oops");
            if (d.TryGetValue("b", out x)) throw new ApplicationException("Oops");
        }
        Console.WriteLine(DateTime.Now-start);
        start = DateTime.Now;
        for (int i = 0; i != 10000000; i++) {
            string x;
            if (d.ContainsKey("a")) {
                x = d["a"];
            } else {
                x = default(string);
            }
            if (d.ContainsKey("b")) {
                x = d["b"];
            } else {
                x = default(string);
            }
        }
   }

这个生产

00:00:00.7600000
00:00:01.0610000

使ContainsKey + Item访问速度慢了40%,假设命中和错过的混合均匀。

此外,当我将程序更改为总是错过(即总是查找“b”)时,两个版本变得同样快:

00:00:00.2850000
00:00:00.2720000

然而,当我让它“all hits”时,TryGetValue仍然是一个明显的赢家:

00:00:00.4930000
00:00:00.8110000

如果你试图从字典中获取值,TryGetValue(key, out value)是最好的选择,但如果你正在检查是否存在键,对于一个新的插入,而不覆盖旧键,并且仅在该范围内,ContainsKey(key)是最好的选择,基准测试可以确认这一点:

using System;
using System.Threading;
using System.Diagnostics;
using System.Collections.Generic;
using System.Collections;

namespace benchmark
{
class Program
{
    public static Random m_Rand = new Random();
    public static Dictionary<int, int> testdict = new Dictionary<int, int>();
    public static Hashtable testhash = new Hashtable();

    public static void Main(string[] args)
    {
        Console.WriteLine("Adding elements into hashtable...");
        Stopwatch watch = Stopwatch.StartNew();
        for(int i=0; i<1000000; i++)
            testhash[i]=m_Rand.Next();
        watch.Stop();
        Console.WriteLine("Done in {0:F4} -- pause....", watch.Elapsed.TotalSeconds);
        Thread.Sleep(4000);
        Console.WriteLine("Adding elements into dictionary...");
        watch = Stopwatch.StartNew();
        for(int i=0; i<1000000; i++)
            testdict[i]=m_Rand.Next();
        watch.Stop();
        Console.WriteLine("Done in {0:F4} -- pause....", watch.Elapsed.TotalSeconds);
        Thread.Sleep(4000);

        Console.WriteLine("Finding the first free number for insertion");
        Console.WriteLine("First method: ContainsKey");
        watch = Stopwatch.StartNew();
        int intero=0;
        while (testdict.ContainsKey(intero))
        {
            intero++;
        }
        testdict.Add(intero, m_Rand.Next());
        watch.Stop();
        Console.WriteLine("Done in {0:F4} -- added value {1} in dictionary -- pause....", watch.Elapsed.TotalSeconds, intero);
        Thread.Sleep(4000);
        Console.WriteLine("Second method: TryGetValue");
        watch = Stopwatch.StartNew();
        intero=0;
        int result=0;
        while(testdict.TryGetValue(intero, out result))
        {
            intero++;
        }
        testdict.Add(intero, m_Rand.Next());
        watch.Stop();
        Console.WriteLine("Done in {0:F4} -- added value {1} in dictionary -- pause....", watch.Elapsed.TotalSeconds, intero);
        Thread.Sleep(4000);
        Console.WriteLine("Test hashtable");
        watch = Stopwatch.StartNew();
        intero=0;
        while(testhash.Contains(intero))
        {
            intero++;
        }
        testhash.Add(intero, m_Rand.Next());
        watch.Stop();
        Console.WriteLine("Done in {0:F4} -- added value {1} into hashtable -- pause....", watch.Elapsed.TotalSeconds, intero);
        Console.Write("Press any key to continue . . . ");
        Console.ReadKey(true);
    }
}
}

这是一个真实的例子,我有一个服务,对于每个创建的“Item”,它关联一个累进的数字,这个数字,每次你创建一个新项目,必须找到空闲的,如果你删除一个Item,空闲的数字变成空闲的,当然这不是优化的,因为我有一个静态的变量,缓存当前的数字,但如果你结束所有的数字,你可以从0到UInt32重新开始。MaxValue

测试执行: 在哈希表中添加元素… 完成于0,5908 -暂停.... 向字典中添加元素… 完成于0,2679 -暂停.... 找到第一个可插入的空闲数 第一种方法:ContainsKey 在0,0561中完成—在字典中添加值1000000—暂停.... 第二个方法:TryGetValue 在0,0643完成—在字典中添加值1000001—暂停.... 测试散列表 在0,3015完成——在哈希表中添加值1000000——暂停.... 按任意键继续。

如果你们中的一些人可能会问ContainsKeys是否有优势,我甚至尝试过用Contains key反转TryGetValue,结果是一样的。

所以,对我来说,最后考虑一下,这一切都取决于程序的行为方式。

除了设计一个能够在实际环境中给出准确结果的微基准测试外,您还可以查看. net Framework的参考源代码。

System.Collections.Generic。字典< TKey, TValue >。TryGetValue(TKey, out TValue) System.Collections.Generic。字典< TKey, TValue > .ContainsKey (TKey) System.Collections.Generic。字典< TKey, TValue > .Item (TKey)

它们都调用FindEntry(TKey)方法,该方法完成大部分工作,并且不记住其结果,因此调用TryGetValue几乎是ContainsKey + Item的两倍快。


TryGetValue不方便的接口可以使用扩展方法进行调整:

using System.Collections.Generic;

namespace Project.Common.Extensions
{
    public static class DictionaryExtensions
    {
        public static TValue GetValueOrDefault<TKey, TValue>(
            this IDictionary<TKey, TValue> dictionary,
            TKey key,
            TValue defaultValue = default(TValue))
        {
            if (dictionary.TryGetValue(key, out TValue value))
            {
                return value;
            }
            return defaultValue;
        }
    }
}

从c# 7.1开始,你可以将default(TValue)替换为plain default。类型是推断出来的。

用法:

var dict = new Dictionary<string, string>();
string val = dict.GetValueOrDefault("theKey", "value used if theKey is not found in dict");

除非指定了显式的默认值,否则查找失败的引用类型将返回null。

var dictObj = new Dictionary<string, object>();
object valObj = dictObj.GetValueOrDefault("nonexistent");
Debug.Assert(valObj == null);

var dictInt = new Dictionary<string, int>();
int valInt = dictInt.GetValueOrDefault("nonexistent");
Debug.Assert(valInt == 0);

制作一个快速测试程序,使用字典中包含100万个项的TryGetValue显然有很大的改进。

结果:

ContainsKey + Item为1000000次:45ms

1000000次的TryGetValue: 26ms

下面是测试应用程序:

static void Main(string[] args)
{
    const int size = 1000000;

    var dict = new Dictionary<int, string>();

    for (int i = 0; i < size; i++)
    {
        dict.Add(i, i.ToString());
    }

    var sw = new Stopwatch();
    string result;

    sw.Start();

    for (int i = 0; i < size; i++)
    {
        if (dict.ContainsKey(i))
            result = dict[i];
    }

    sw.Stop();
    Console.WriteLine("ContainsKey + Item for {0} hits: {1}ms", size, sw.ElapsedMilliseconds);

    sw.Reset();
    sw.Start();

    for (int i = 0; i < size; i++)
    {
        dict.TryGetValue(i, out result);
    }

    sw.Stop();
    Console.WriteLine("TryGetValue for {0} hits: {1}ms", size, sw.ElapsedMilliseconds);

}