You need to implement ICloneable.
Define your dictionary as a class, derived from Dictionary:
public class CloneableDictionary<TKey, TValue> : Dictionary<TKey, TValue> where TValue : ICloneable
{
public CloneableDictionary<TKey, TValue> Clone()
{
CloneableDictionary<TKey, TValue> clone = new CloneableDictionary<TKey, TValue>();
foreach (KeyValuePair<TKey, TValue> kvp in this)
{
clone.Add(kvp.Key, (TValue) kvp.Value.Clone());
}
return clone;
}
}
Then you can use the Clone method:
CloneableDictionary<int, string> dic = new CloneableDictionary<int, string>() {
{1, "Key1"},
{2, "Key2"}};
CloneableDictionary<int, string> dicCopy = dic.Clone();
dicCopy[1] = "Key Changed";
foreach (KeyValuePair<int, string> kvp in dic)
{
Console.WriteLine(kvp.Key.ToString() + ":" + kvp.Value);
}
foreach (KeyValuePair<int, string> kvp in dicCopy)
{
Console.WriteLine(kvp.Key.ToString() + ":" + kvp.Value);
}