Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble with type inference in writing generic extension method

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.

like image 827
Simon_Weaver Avatar asked Dec 31 '25 23:12

Simon_Weaver


1 Answers

You seem to be defining your extension method with three generic types when you only need two. "TValue" and "TType" mean the same thing, don't they? Try this:

public static TValue GetValueOrDefault<TKey, TValue>(
    this IDictionary<TKey, TValue> dictionary, TKey key)
{
    TValue value;
    // attempt to get the value of the key from the dictionary
    dictionary.TryGetValue(key, out value);
    return value;
}    
like image 125
Matt Hamilton Avatar answered Jan 02 '26 12:01

Matt Hamilton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!