Используйте метод расширения:
static class DictionaryExtensions {
public static TValue GetValueOrDefault<TKey, TValue>(this Dictionary<TKey,TValue> dic, TKey key, Func<TKey, TValue> valueGenerator) {
TValue val;
if (dic.TryGetValue(key, out val))
return val;
return valueGenerator(key);
}
}
Вы можете позвонить с помощью:
dic.GetValueOrDefault("nonexistent key", key => "null");
Или передать функцию-член:
dic.GetValueOrDefault("nonexistent key", MyMemberFunction);