C# 检索字典值最佳实践

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/378465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 01:14:10  来源:igfitidea点击:

Retrieving Dictionary Value Best Practices

c#.netdictionary

提问by Nicholas Mancuso

I just recently noticed Dictionary.TryGetValue(TKey key, out TValue value)and was curious as to which is the better approach to retrieving a value from the Dictionary.

我最近才注意到Dictionary.TryGetValue(TKey key, out TValue value)并很好奇从字典中检索值的更好方法是什么。

I've traditionally done:

我传统上做过:

if (myDict.Contains(someKey))
     someVal = myDict[someKey];
     ...

unless I know it hasto be in there.

除非我知道它必要在里面。

Is it better to just do:

是否更好地执行以下操作:

if (myDict.TryGetValue(somekey, out someVal)
    ...

Which is the better practice? Is one faster than the other? I would imagine that the Try version would be slower as its 'swallowing' a try/catch inside itself and using that as logic, no?

哪个是更好的做法?一个比另一个快吗?我会想象 Try 版本会更慢,因为它会“吞下”自身内部的 try/catch 并将其用作逻辑,不是吗?

采纳答案by Micah

TryGetValue is slightly faster, because FindEntry will only be called once.

TryGetValue 稍微快一点,因为 FindEntry 只会被调用一次。

How much faster? It depends on the dataset at hand. When you call the Contains method, Dictionary does an internal search to find its index. If it returns true, you need another index search to get the actual value. When you use TryGetValue, it searches only once for the index and if found, it assigns the value to your variable.

快多少?这取决于手头的数据集。当您调用 Contains 方法时,Dictionary 会进行内部搜索以查找其索引。如果它返回 true,则需要另一个索引搜索来获取实际值。当您使用 TryGetValue 时,它​​只搜索索引一次,如果找到,它会将值分配给您的变量。

FYI: It's not actually catching an error.

仅供参考:实际上并没有发现错误。

It's calling:

它在呼唤:

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 is this:

包含密钥是这样的:

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

回答by Jennifer

I imagine that trygetvalue is doing something more like:

我想 trygetvalue 正在做的事情更像是:

if(myDict.ReallyOptimisedVersionofContains(someKey))
{ 
  someVal = myDict[someKey];
  return true;
}
return false;

So hopefully no try/catch anywhere.

所以希望不要在任何地方尝试/捕获。

I think it is just a method of convenience really. I generally use it as it saves a line of code or two.

我认为这真的只是一种方便的方法。我通常使用它,因为它可以节省一两行代码。

回答by Diadistis

Well in fact TryGetValue is faster. How much faster? It depends on the dataset at hand. When you call the Contains method, Dictionary does an internal search to find its index. If it returns true, you need another index search to get the actual value. When you use TryGetValue, it searches only once for the index and if found, it assigns the value to your variable.

事实上,TryGetValue 更快。快多少?这取决于手头的数据集。当您调用 Contains 方法时,Dictionary 会进行内部搜索以查找其索引。如果它返回 true,则需要另一个索引搜索来获取实际值。当您使用 TryGetValue 时,它​​只搜索索引一次,如果找到,它会将值分配给您的变量。

Edit:

编辑:

Ok, I understand your confusion so let me elaborate:

好的,我理解你的困惑,所以让我详细说明:

Case 1:

情况1:

if (myDict.Contains(someKey))
     someVal = myDict[someKey];

In this case there are 2 calls to FindEntry, one to check if the key exists and one to retrieve it

在这种情况下,有 2 次调用 FindEntry,一次是检查密钥是否存在,另一次是检索它

Case 2:

案例2:

myDict.TryGetValue(somekey, out someVal)

In this case there is only one call to FindKey because the resulting index is kept for the actual retrieval in the same method.

在这种情况下,只有一次对 FindKey 的调用,因为在同一方法中为实际检索保留了结果索引。