title: NULL coalescing Dictonary lookup
body: <p>One thing that always bugs me is how many lines of code it takes to do this very simple (and for my work, very common) operation:</p>
  
  <pre><code>  var lTheDict = new Dictionary&lt;string, object&gt;();
    // The dictionary gets some stuff put in it elsewhere...
  
    // Do annoying lookup that must be common but is always unwieldy.
    object lTheObject;
    int lTheValue; // NOTE: Not always an int
    if (lTheDict.TryGetValue("TheKey", out lTheObject))
    {
      lTheValue = (int) lTheObject;
    }
  </code></pre>
  
  <p>I'm sure there must be a better way to do this, perhaps with null coalescing or something. What I'd really like to be able to write is something like:</p>
  
  <pre><code>  int lTheValue ?= (int) lTheDict["TheKey"];
  </code></pre>
  
  <p>In other words, if TheKey exists, get me the value, otherwise just give me the "empty" int.</p>
  
  <p>It's almost impossible to shave lines off. Even if we just attempt the lookup-and-cast and catch the exception on a single line catch-and-ignore (none of which are permitted by my workplace code style), we'd <em>still</em> have to declare the variable outside the try block, and end up with something like:</p>
  
  <pre><code>  int lTheValue;
    try {
      lTheValue = (int) lTheDict["TheKey"];
    } catch (Exception ex) { }
  </code></pre>
  
  <p>Which is again a ridiculous amount of overhead code masking what should be a pretty obvious operation.</p>
  
  <p>Even just to be able to get rid of the declaration of lTheObject by declaring it at the point where we use it as an outparam (which is coming in .NET 5 or something I hear) would remove one line. The functions where this lives are often only 10 lines long themselves, and it looks like we're doing something important with this collection because half of the code is devoted to just getting out a value, but really it's just a distraction.</p>
  
  <p><strong>NOTE:</strong> I know I can write a templated function to do it, but even in the function it's going to annoy me having to write these lines AGAIN. There must be a better way!</p>
  
  <p>Has anyone found, or can you think of, a shorter way to write this?</p>