Q:

Want to Simplify C# Access to Dictionary When Key is Tuple

Is there a simpler way to check a dictionary where the key is a tuple and then assign the value you were just checking for to a variable?  I've got the following code that works, but it seems tedious.
int hitCount = dict.ContainsKey(new Tuple<string, string>(tr, "H"))
   ? dict[new Tuple<string, string>(tr, "H")]
   : 0;

A:

int hitCount;
var key = new Tuple<string, string>(tr, "H");
dict.TryGetValue(key, out hitCount);

Starting with C# 7:
var key = new Tuple<string, string>(tr, "H");
dict.TryGetValue(key, out int hitCount);

The difference is that the declaration of hitCount is moved to the place where it's used as an out parameter.
It's obviously not really necessary to define the key in a separate variable, but in these scenarios you're likely going to want to add the key if it doesn't exist.

A:

Use Tuple.Create to let the compiler infer generic type parameters from the type of method arguments:
int hitCount;
if (!dict.TryGetValue(Tuple.Create(tr, "H"), out hitCount)) {
    hitCount = 0;
}

Note: Setting hitCount to zero is not necessary. This code illustrates how to deal with missing keys, in case you wish to use a different number as a default.

