This goes against what I thought I understood about copying a Dictionary. Say, I have the following code:
public class MyClass
{
public string str1;
public MyClass(string s)
{
str1 = s;
}
}
Dictionary<string, MyClass> dic1 = new Dictionary<string, MyClass>();
dic1.Add("0", new MyClass("hello"));
//Make 'dic2' as a copy of 'dic1'
Dictionary<string, MyClass> dic2 = new Dictionary<string, MyClass>(dic1);
//Alter 'dic1'
dic1.ElementAt(0).Value.str1 += "!!!";
//I was expecting dic2 not to be altered, but IT IS!
Debug.Assert(dic2["0"].str1.Equals(dic1["0"].str1, StringComparison.Ordinal) == false); //Result is true for equality
I was expecting that changing a copied dictionary I would not change the original one, but that is not the case with my code.
What am I doing wrong?