If you're using .NET's IDictionary<K, V> you have probably found its access API too boring. Indeed at each access point you have to write a code like this:
IDictionary<K, V>
MyValueType value; var hasValue = dictionary.TryGetValue(key, out value); ...
In many, if not in most, cases the value is of a reference type, and you do not usually store null values, so it would be fine if dictionary returned null when value does not exist for the key.
null
To deal with this small nuisance we have declared a couple of accessor extension methods:
public static class Extensions { public static V Get<K, V>(this IDictionary<K, V> dictionary, K key) where V: class { V value; if (key == null) { value = null; } else { dictionary.TryGetValue(key, out value); } return value; } public static V Get<K, V>(this IDictionary<K, V> dictionary, K? key) where V: class where K: struct { V value; if (key == null) { value = null; } else { dictionary.TryGetValue(key.GetValueOrDefault(), out value); } return value; } }
These methods simplify dictionary access to:
var value = dictionary.Get(key); ...
Remember Me
a@href@title, b, blockquote@cite, em, i, strike, strong, sub, super, u