许多文章都推荐大家在C#的Dictionary中使用TryGetValue而不是先ContainsKey判断然后再取值,通过源码我们很容易理解:
这是TryGetValue的源码:
public bool TryGetValue(TKey key, out TValue value)
{
if (key == null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
}
lock(_lock)
{
VerifyIntegrity();
return TryGetValueWorker(key, out value);
}
}
private bool TryGetValueWorker(TKey key, out TValue value)
{
int entryIndex = FindEntry(key);
if (entryIndex != -1)
{
Object primary = null;
Object secondary = null;
_entries[entryIndex].depHnd.GetPrimaryAndSecondary(out primary, out secondary);
// Now that we've secured a strong reference to the secondary, must check the primary again
// to ensure it didn't expire (otherwise, we open a ---- where TryGetValue misreports an
// expired key as a live key with a null value.)
if (primary != null)
{
value = (TValue)secondary;
return true;
}
}
value = default(TValue);
return false;
}
这是ContainsKey和字典索引的源码:
public bool ContainsKey(TKey key) {
return FindEntry(key) >= 0;
}
public TValue this[TKey key] {
get {
int i = FindEntry(key);
if (i >= 0) return entries[i].value;
ThrowHelper.ThrowKeyNotFoundException();
return default(TValue);
}
set {
Insert(key, value, false);
}
}
我们可以看到,先判断key是否存在然后再取值,需要调用两次FindEntry(),因此TryGetValue通常效率约为判断再取值的二倍。
但是,我在某些情况下发现TryGetValue方法速度非常慢,经过测试发现TryGetValue的速度仅为判断再取值的十分之一。这是因为当Dictionary的value是复杂对象的时候,TryGetValue会将value转换为Object再转换为对应类型,这个装箱拆箱过程对复杂对象耗时很高。而字典索引的方法会直接将value的对象返回。
因此,当字典的value是复杂对象的时候,建议大家不要使用TryGetValue。