title: What is more efficient: Dictionary TryGetValue or ContainsKey+Item?
body: <p>From MSDN's entry on <a href="http://msdn.microsoft.com/en-us/library/bb347013.aspx" rel="noreferrer">Dictionary.TryGetValue Method</a>:</p>
  
  <blockquote>
    <p>This method combines the functionality of the ContainsKey method and
    the Item property.</p>
    
    <p>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.</p>
    
    <p>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.</p>
    
    <p>This method approaches an O(1) operation.</p>
  </blockquote>
  
  <p>From the description, it's not clear if it is more efficient or just more convenient than calling ContainsKey and then doing the lookup. Does the implementation of <code>TryGetValue</code> just call ContainsKey and then Item or is actually more efficient than that by doing a single lookup?</p>
  
  <p>In other words, what is more efficient (i.e. which one performs less lookups):</p>
  
  <pre><code>Dictionary&lt;int,int&gt; dict;
  //...//
  int ival;
  if(dict.ContainsKey(ikey))
  {
    ival = dict[ikey];
  }
  else
  {
    ival = default(int);
  }
  </code></pre>
  
  <p>or</p>
  
  <pre><code>Dictionary&lt;int,int&gt; dict;
  //...//
  int ival;
  dict.TryGetValue(ikey, out ival);
  </code></pre>
  
  <p>Note: I am not looking for a benchmark!</p>