Simon_Weaver
Simon_Weaver

Reputation: 145880

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.

Upvotes: 3

Views: 707

Answers (1)

Matt Hamilton
Matt Hamilton

Reputation: 204129

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;
}    

Upvotes: 5

Related Questions