Which mechanism is a better way to extend Dictionary to deal with missing keys and why?

后端 未结 3 1318
一整个雨季
一整个雨季 2021-01-04 00:48

There is a minor annoyance I find myself with a lot - I have a Dictionary that contains values that may or may not be there.

So norm

3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-04 01:29

    Or perhaps

    public static TValue TryGet(this Dictionary input, 
                                                       TKey key)
    {
    
         return input.ContainsKey(key) ? input[key] : *some default value*;
    
    }
    

提交回复
热议问题