Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var x = dict.ContainsKey(key) ? dict[key] : defaultValue
- var x = dict[key] ?? defaultValue;
- public static class MyHelper
- {
- public static V GetValueOrDefault<K, V>(this Dictionary<K, V> dic, K key, V defaultVal)
- {
- V ret;
- bool found = dic.TryGetValue(key, out ret);
- if (found) { return ret; }
- return defaultVal;
- }
- void Example()
- {
- var dict = new Dictionary<int, string>();
- dict.GetValueOrDefault(42, "default");
- }
- }
- public abstract class MyHelper {
- public static V GetValueOrDefault<K,V>( Dictionary<K,V> dic, K key ) {
- V ret;
- bool found = dic.TryGetValue( key, out ret );
- if ( found ) { return ret; }
- return default(V);
- }
- }
- var x = MyHelper.GetValueOrDefault( dic, key );
Add Comment
Please, Sign In to add comment