3

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.

1 Answer 1

5

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; } 
Sign up to request clarification or add additional context in comments.

1 Comment

thx guys. nicely simplified. and duh - i knew 'value' was an 'out' param so i don't know why i initialized it. i typed 'out' but was thinking 'ref'.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.