I really hate sometimes how IDictionary<TKey, TValue> [key]
will throw an exception if the key doesn't exist in the dictionary.
Of course there is TryGetValue()
, but that seems to have been optimized for performance and not usability.
So I thought, oh I'll just make an extension method for it - which I did :
public static class CollectionExtensions
{
public static TType GetValueOrDefault<TKeyType, TValue, TType>(this IDictionary<TKeyType, TType> dictionary, TKeyType key)
{
TType value = default(TType);
// attempt to get the value of the key from the dictionary
// if the key doesn't exist just return null
if (dictionary.TryGetValue(key, out value))
{
return value;
}
else
{
return default(TType);
}
}
}
This works fine EXCEPT I cannot seem to get type inference working.
Obviously I want to be able to do the following :
var extraDataLookup = new Dictionary<string, string>();
extraDataLookup["zipcode"] = model.Zipcode;
and then be able to access the value :
var zipcode = extraDataLookup.GetValueOrDefault("zipcode");
var foo = extraDataLookup.GetValueOrDefault("foo"); // should be null
I've looked at a few things about type inference, inlucing Jon Skeet's article and even sourcecode to System.Linq.Enumerable
in reflector but seem to be missing something.
This works :
extraDataLookup.GetValueOrDefault<string, string,string> ("foo")
but this doesn't
extraDataLookup.GetValueOrDefault ("foo")
What should I be doing.
PS. I'm only looking for solutions to the generic type inference problem, not any other suggestions. Thanks.