Roc*_*ngh 2 .net c# dictionary .net-4.0
我有以下代码:
Dictionary<int, int> test = new Dictionary<int, int>();
test.Add(1,1);
test.Add(2, 2);
Dictionary<int, int> test2 = test;
test2.Remove(1);
Run Code Online (Sandbox Code Playgroud)
从test2中删除项目也是从测试对象中删除项目.你能告诉我如何在不影响测试的情况下修改test2中的项目吗?
test2和test是对同一个对象(字典)的相同引用.为test2实例化一个新字典.
Dictionary<int, int> test2 = new Dictionary<int, int>(test);
Run Code Online (Sandbox Code Playgroud)