Retrieving Dictionary Value Best Practices

前端 未结 3 856
遥遥无期
遥遥无期 2020-12-24 04:22

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 Dic

3条回答
  •  生来不讨喜
    2020-12-24 04:52

    I imagine that trygetvalue is doing something more like:

    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.

提交回复
热议问题