开发者

deep copy hashtable

开发者 https://www.devze.com 2023-01-23 15:08 出处:网络
How do I deep copy a hashtable to another开发者_如何学Go new hashtable?How about this: public void CloneDict(Dictionary<K,V> dictionary) where V:IClonable

How do I deep copy a hashtable to another开发者_如何学Go new hashtable?


How about this:

public void CloneDict(Dictionary<K,V> dictionary) where V:IClonable 
{
     Dictionary<K,V> clonedOne = new Dictionary<K,V>();

     foreach(KeyValuePair<K,V> pair in dictoinary) {
         clonedOne(pair.Key, (V) pair.Value.Clone()
     }
}

Then implement the IClonable for whatever objects you are to store in the dictionary. Same approach can be applied to Hashtable but only as non-generic.


You can create a new one with the old as parameter:

Dictionary<a,b> dic=new Dictionary<a,b>(oldDict)

and the same for HashSet<T>. This constructs a new hashtable from the contents of the old one.

Note: If you're not using the default comparer you need to specify it again in the constructor.

Dictionary<TKey, TValue>(IDictionary<TKey, TValue>) Initializes a new instance of the Dictionary class that contains elements copied from the specified IDictionary and uses the default equality comparer for the key type.

0

精彩评论

暂无评论...
验证码 换一张
取 消