我发现自己现在经常在我的代码中使用当前模式
var dictionary = new Dictionary<type, IList<othertype>>();
// Add stuff to dictionary
var somethingElse = dictionary.ContainsKey(key) ? dictionary[key] : new List<othertype>();
// Do work with the somethingelse variable
Run Code Online (Sandbox Code Playgroud)
或者有时候
var dictionary = new Dictionary<type, IList<othertype>>();
// Add stuff to dictionary
IList<othertype> somethingElse;
if(!dictionary.TryGetValue(key, out somethingElse) {
somethingElse = new List<othertype>();
}
Run Code Online (Sandbox Code Playgroud)
这两种方式都让人觉得很迂回.我真正想要的是这样的
dictionary.GetValueOrDefault(key)
Run Code Online (Sandbox Code Playgroud)
现在,我可以为字典类编写一个扩展方法来为我做这个,但我想我可能会遗漏已经存在的东西.那么,有没有办法以更简单的方式做到这一点,而无需在字典中编写扩展方法?
在某些情况下,当字典中没有这样的键时,对于我来说,有一个简短的,可读的方式来获取null而不是KeyNotFoundException按键访问字典值,这似乎是有用的.
我想到的第一件事是扩展方法:
public static U GetValueByKeyOrNull<T, U>(this Dictionary<T, U> dict, T key)
where U : class //it's acceptable for me to have this constraint
{
if (dict.ContainsKey(key))
return dict[key];
else
//it could be default(U) to use without U class constraint
//however, I didn't need this.
return null;
}
Run Code Online (Sandbox Code Playgroud)
但是当你写下这样的东西时,它实际上并不是很短暂的说法:
string.Format("{0}:{1};{2}:{3}",
dict.GetValueByKeyOrNull("key1"),
dict.GetValueByKeyOrNull("key2"),
dict.GetValueByKeyOrNull("key3"),
dict.GetValueByKeyOrNull("key4"));
Run Code Online (Sandbox Code Playgroud)
我会说,有一些接近基本语法的东西要好得多:dict["key4"].
然后我想出了一个带有private字典字段的类的想法,它暴露了我需要的功能:
public class MyDictionary<T, U> //here I may add any of interfaces, implemented
//by dictionary itself to …Run Code Online (Sandbox Code Playgroud) 我经常发现自己创建了Dictionary一个非平凡的值类(例如List),然后在填充数据时总是编写相同的代码模式.
例如:
var dict = new Dictionary<string, List<string>>();
string key = "foo";
string aValueForKey = "bar";
Run Code Online (Sandbox Code Playgroud)
也就是说,我想插入"bar"与key对应的列表"foo",其中key "foo"可能不会映射到任何内容.
这是我使用不断重复的模式的地方:
List<string> keyValues;
if (!dict.TryGetValue(key, out keyValues))
dict.Add(key, keyValues = new List<string>());
keyValues.Add(aValueForKey);
Run Code Online (Sandbox Code Playgroud)
有更优雅的方式吗?
相关问题没有这个问题的答案:
我真的很烦我,我必须使用.ContainsKey而不是仅仅value==null在字典中做一个.真人生活的例子:
var dictionary = new Dictionary<string, FooBar>();
var key = "doesnt exist";
var tmp = dictionary[key] ?? new FooBar(x, y);
Run Code Online (Sandbox Code Playgroud)
相反,我有这些选择:
var key = "doesnt exist";
FooBar tmp = null;
if (dictionary.ContainsKey(key))
{
tmp = dictionary[key];
} else {
tmp = new FooBar(x, y);
}
Run Code Online (Sandbox Code Playgroud)
或这个:
FooBar tmp = null;
if (!Dictionary.TryGetValue(key, out tmp))
{
tmp = new FooBar(x, y);
}
Run Code Online (Sandbox Code Playgroud)
对我来说,这段代码非常冗长.是否有一个内置的泛型类实现IDictionary(或以其他方式允许键值类型访问)但在我尝试查找键时不会抛出异常,而是返回null?
我有以下 PageViewModel 类:
public class PageViewModel : ViewModel<Page>
{
public PageViewModel ()
{
Kywords = new List<Keyword>();
AnswserKeywordDictionary = new Dictionary<string, Answer>();
}
public Company Company { get; set; }
public List<Keyword> Kywords { get; set; }
public Dictionary<string, Answer> AnswserKeywordDictionary { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
在我看来,我正在使用 AnswserKeywordDictionary 属性,如下所示:
@Html.DisplayAffirmativeAnswer(Model.AnswserKeywordDictionary["myKey"])
Run Code Online (Sandbox Code Playgroud)
我的问题是:如果“myKey”不在字典中,我如何返回默认值。
提前致谢