There doesn't seem to be a dictionary.AddRange() method. Does anyone know a better way to copy the items to another dictionary without using a foreach loop.
似乎没有dictionary.AddRange()方法。有没有人知道在不使用foreach循环的情况下将项目复制到另一个字典的更好方法。
I'm using the System.Collections.Generic.Dictionary. This is for .NET 2.0.
我正在使用System.Collections.Generic.Dictionary。这适用于.NET 2.0。
6 个解决方案
#1
15
There's nothing wrong with a for/foreach loop. That's all a hypothetical AddRange method would do anyway.
for / foreach循环没有任何问题。无论如何,这都是假设的AddRange方法。
The only extra concern I'd have is with memory allocation behaviour, because adding a large number of entries could cause multiple reallocations and re-hashes. There's no way to increase the capacity of an existing Dictionary by a given amount. You might be better off allocating a new Dictionary with sufficient capacity for both current ones, but you'd still need a loop to load at least one of them.
我唯一需要关注的是内存分配行为,因为添加大量条目可能会导致多次重新分配和重新散列。没有办法按给定的数量增加现有词典的容量。你可能最好分配一个具有足够容量的新词典用于当前的词典,但你仍然需要一个循环来加载至少其中一个。
#2
19
There's the Dictionary
constructor that takes another Dictionary
.
还有Dictionary构造函数,它接受另一个Dictionary。
You'll have to cast it IDictionary
, but there is an Add()
overload that takes KeyValuePair<TKey, TValue>
. You're still using foreach, though.
你必须将它转换为IDictionary,但是有一个Add()重载,它接受KeyValuePair
#3
6
var Animal = new Dictionary<string, string>();
one can pass existing animal Dictionary to the constructor.
可以将现有的动物词典传递给构造函数。
Dictionary<string, string> NewAnimals = new Dictionary<string, string>(Animal);
#4
3
For fun, I created this extension method to dictionary. This should do a deep copy wherever possible.
为了好玩,我创建了这个扩展方法到字典。这应该尽可能地进行深层复制。
public static Dictionary<TKey, TValue> DeepCopy<TKey,TValue>(this Dictionary<TKey, TValue> dictionary)
{
Dictionary<TKey, TValue> d2 = new Dictionary<TKey, TValue>();
bool keyIsCloneable = default(TKey) is ICloneable;
bool valueIsCloneable = default(TValue) is ICloneable;
foreach (KeyValuePair<TKey, TValue> kvp in dictionary)
{
TKey key = default(TKey);
TValue value = default(TValue);
if (keyIsCloneable)
{
key = (TKey)((ICloneable)(kvp.Key)).Clone();
}
else
{
key = kvp.Key;
}
if (valueIsCloneable)
{
value = (TValue)((ICloneable)(kvp.Value)).Clone();
}
else
{
value = kvp.Value;
}
d2.Add(key, value);
}
return d2;
}
#5
0
If you're dealing with two existing objects, you might get some mileage with the CopyTo method: http://msdn.microsoft.com/en-us/library/cc645053.aspx
如果您正在处理两个现有对象,您可能会使用CopyTo方法获得一些里程:http://msdn.microsoft.com/en-us/library/cc645053.aspx
Use the Add method of the other collection (receiver) to absorb them.
使用其他集合(接收器)的Add方法吸收它们。
#6
0
I don't understand, why not using the Dictionary( Dictionary ) (as suggested by ageektrapped ).
我不明白,为什么不使用Dictionary(Dictionary)(由ageektrapped建议)。
Do you want to perform a Shallow Copy or a Deep Copy? (that is, both Dictionaries pointing to the same references or new copies of every object inside the new dictionary?)
您想要执行浅拷贝还是深拷贝? (也就是说,两个词典都指向相同的引用或新词典中每个对象的新副本?)
If you want to create a new Dictionary pointing to new objects, I think that the only way is through a foreach.
如果你想创建一个指向新对象的新词典,我认为唯一的方法是通过foreach。
#1
15
There's nothing wrong with a for/foreach loop. That's all a hypothetical AddRange method would do anyway.
for / foreach循环没有任何问题。无论如何,这都是假设的AddRange方法。
The only extra concern I'd have is with memory allocation behaviour, because adding a large number of entries could cause multiple reallocations and re-hashes. There's no way to increase the capacity of an existing Dictionary by a given amount. You might be better off allocating a new Dictionary with sufficient capacity for both current ones, but you'd still need a loop to load at least one of them.
我唯一需要关注的是内存分配行为,因为添加大量条目可能会导致多次重新分配和重新散列。没有办法按给定的数量增加现有词典的容量。你可能最好分配一个具有足够容量的新词典用于当前的词典,但你仍然需要一个循环来加载至少其中一个。
#2
19
There's the Dictionary
constructor that takes another Dictionary
.
还有Dictionary构造函数,它接受另一个Dictionary。
You'll have to cast it IDictionary
, but there is an Add()
overload that takes KeyValuePair<TKey, TValue>
. You're still using foreach, though.
你必须将它转换为IDictionary,但是有一个Add()重载,它接受KeyValuePair
#3
6
var Animal = new Dictionary<string, string>();
one can pass existing animal Dictionary to the constructor.
可以将现有的动物词典传递给构造函数。
Dictionary<string, string> NewAnimals = new Dictionary<string, string>(Animal);
#4
3
For fun, I created this extension method to dictionary. This should do a deep copy wherever possible.
为了好玩,我创建了这个扩展方法到字典。这应该尽可能地进行深层复制。
public static Dictionary<TKey, TValue> DeepCopy<TKey,TValue>(this Dictionary<TKey, TValue> dictionary)
{
Dictionary<TKey, TValue> d2 = new Dictionary<TKey, TValue>();
bool keyIsCloneable = default(TKey) is ICloneable;
bool valueIsCloneable = default(TValue) is ICloneable;
foreach (KeyValuePair<TKey, TValue> kvp in dictionary)
{
TKey key = default(TKey);
TValue value = default(TValue);
if (keyIsCloneable)
{
key = (TKey)((ICloneable)(kvp.Key)).Clone();
}
else
{
key = kvp.Key;
}
if (valueIsCloneable)
{
value = (TValue)((ICloneable)(kvp.Value)).Clone();
}
else
{
value = kvp.Value;
}
d2.Add(key, value);
}
return d2;
}
#5
0
If you're dealing with two existing objects, you might get some mileage with the CopyTo method: http://msdn.microsoft.com/en-us/library/cc645053.aspx
如果您正在处理两个现有对象,您可能会使用CopyTo方法获得一些里程:http://msdn.microsoft.com/en-us/library/cc645053.aspx
Use the Add method of the other collection (receiver) to absorb them.
使用其他集合(接收器)的Add方法吸收它们。
#6
0
I don't understand, why not using the Dictionary( Dictionary ) (as suggested by ageektrapped ).
我不明白,为什么不使用Dictionary(Dictionary)(由ageektrapped建议)。
Do you want to perform a Shallow Copy or a Deep Copy? (that is, both Dictionaries pointing to the same references or new copies of every object inside the new dictionary?)
您想要执行浅拷贝还是深拷贝? (也就是说,两个词典都指向相同的引用或新词典中每个对象的新副本?)
If you want to create a new Dictionary pointing to new objects, I think that the only way is through a foreach.
如果你想创建一个指向新对象的新词典,我认为唯一的方法是通过foreach。