C# C语言中带有列表的默认字典#
除了自己创建这样一个类之外,有没有办法在C#中创建一个C# C语言中带有列表的默认字典#,c#,dictionary,C#,Dictionary,除了自己创建这样一个类之外,有没有办法在C#中创建一个字典,它会自动生成一个与任何键关联的空列表?如果可能,我希望避免使用以下代码: int x = 0; int y = 42; Dictionary<int, List<int>> dict = new Dictionary<int, List<int>>(); List<int> newList; if (dict.containsKey(x)) { dict[x].Add
字典
,它会自动生成一个与任何键关联的空列表?如果可能,我希望避免使用以下代码:
int x = 0;
int y = 42;
Dictionary<int, List<int>> dict = new Dictionary<int, List<int>>();
List<int> newList;
if (dict.containsKey(x))
{
dict[x].Add(y);
}
else
{
dict[x] = new List<int>{y};
}
intx=0;
int y=42;
Dictionary dict=新字典();
列表新建列表;
if(dict.containsKey(x))
{
dict[x]。添加(y);
}
其他的
{
dict[x]=新列表{y};
}
或者可能:
int x = 0;
int y = 42;
Dictionary<int, List<int>> dict = new Dictionary<int, List<int>>();
List<int> newList;
if (dict.TryGetValue(x, out newList))
{
newList.Add(y);
}
else
{
dict[x] = new List<int>{y};
}
intx=0;
int y=42;
Dictionary dict=新字典();
列表新建列表;
if(dict.TryGetValue(x,out newList))
{
添加(y);
}
其他的
{
dict[x]=新列表{y};
}
通常我的代码是:
if (!dict.containsKey(x))
dict[x] = new List<int>();
dict[x].Add(y);
if(!dict.containsKey(x))
dict[x]=新列表();
dict[x]。添加(y);
我相信阅读起来更容易通常我的代码是:
if (!dict.containsKey(x))
dict[x] = new List<int>();
dict[x].Add(y);
if(!dict.containsKey(x))
dict[x]=新列表();
dict[x]。添加(y);
我相信它更容易阅读你已经有了答案,字典没有这样的能力,使用扩展方法只是伪造它,并隐藏你正在查找字典两次的事实(以防键不存在)。但是,如果这让您感到高兴,您将编写一行代码 在c#7中,你怎么能把东西多装一点
if (dict.TryGetValue(x, out var list)) list.Add(y);
else dict.Add(x, new List<int>{y});
if(dict.TryGetValue(x,out var list))list.Add(y);
else dict.Add(x,新列表{y});
如果键已经存在,这将有一个查找,如果键不存在,将有两个查找。您已经有了答案,字典没有这样的功能,使用扩展方法只是伪造它并隐藏您正在查找字典两次的事实(以防键不存在)。但是,如果这让您感到高兴,您将编写一行代码 在c#7中,你怎么能把东西多装一点
if (dict.TryGetValue(x, out var list)) list.Add(y);
else dict.Add(x, new List<int>{y});
if(dict.TryGetValue(x,out var list))list.Add(y);
else dict.Add(x,新列表{y});
如果键已经存在,则有一个查找;如果键不存在,则有两个查找。使用扩展方法可以很好地抽象此行为
public static TValue GetOrAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, Func<TKey, TValue> valueFactory)
{
if (dictionary == null) throw new ArgumentNullException(nameof(dictionary));
if (valueFactory == null) throw new ArgumentNullException(nameof(valueFactory));
TValue value;
if (!dictionary.TryGetValue(key, out value))
{
value = valueFactory.Invoke(key);
dictionary.Add(key, value);
}
return value;
}
这种行为可以通过扩展方法很好地抽象出来
public static TValue GetOrAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, Func<TKey, TValue> valueFactory)
{
if (dictionary == null) throw new ArgumentNullException(nameof(dictionary));
if (valueFactory == null) throw new ArgumentNullException(nameof(valueFactory));
TValue value;
if (!dictionary.TryGetValue(key, out value))
{
value = valueFactory.Invoke(key);
dictionary.Add(key, value);
}
return value;
}
您可以将上述代码封装在扩展中method@Harold有道理,我只是想检查是否有内置的方法,就像python中的方法一样。
Dictionary
没有,但是Loyc
库中的MMap
有AddOrFind
方法。如果您的密钥事先已知,那么.ToDictionary
可能会有所帮助:可枚举的.Range(1,10).ToDictionary(x=>x,y=>newlist())
您可以将上述代码包装在一个扩展中method@Harold有道理,我只是想看看是否有一个内置的方式,就像python中一样。Dictionary
没有这个功能,但是Loyc
库中的MMap
有AddOrFind
方法。如果您的键事先已知,可能会有帮助:Enumerable.Range(1,10)。ToDictionary(x=>x,y=>new List())
使用TryGetValue
时,这会导致2次查找,而不是1次。它还会在“最初不包含键”中添加两次y
case…@owairc:我不认为你真的回复了我的评论,尽管你现在已经修复了代码。但是,如果您试图回复xxbbcc的评论,则在编译时不会对其进行优化。确实会有两次查找。这会导致在使用TryGetValue
时查找2次而不是1次。它还在“最初不包含键”的情况下添加了两次y
。@owairc:我认为你没有真正回复我的评论,尽管你现在已经修复了代码。但是,如果您试图回复xxbbcc的评论,则在编译时不会对其进行优化。确实会有两次查找。