如何在C#中创建字典备份 [英] How to create a backup of dictionary in C#

查看:77
本文介绍了如何在C#中创建字典备份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我有一个带字典的字典< int,string> dict_1和另一个作为相同类型dict_2的备份。 dict_1中的任何更改都不得反映在dict_2中。当用户想要恢复原始字典时,dict_2必须复制到dict_1。请帮帮我。



我尝试过:



尝试下面的代码,但无法工作,

Hello,

I have a Dictionary with Dictionary<int,string> dict_1 and another as a backup with same type dict_2. Any changes in dict_1 must NOT reflect in dict_2. when user wants to revert the original dictionary, dict_2 must copied to dict_1. Please help me on this.

What I have tried:

Tried the below code but couldn't work,

Dictionary<int,string> dict_1 =new Dictionary<int, string="">();
dict_1.Add(1,"a");
dict_1.Add(1,"b");
dict_1.Add(1,"c");
Dictionary<int, string=""> dict_2 =new Dictionary<int,string>(dict_1);
dict_1.Remove(1);//affects dict_2 also which should not happen.

推荐答案

第一步是修复你的代码,使其编译。 />


第二步是修复代码,使其运行。



第三步是调试并观察发生了什么。



例如你会得到类似的结果(使用Console.WriteLine,当然你会使用调试器)

First step is to fix your code so it compiles.

Second step is to fix the code so it runs.

The third step is to debug it and observe what is going on.

You will for example end up with something like this (using Console.WriteLine, of course you would use a debugger)
using System;
using System.Collections.Generic;
					
public class Program
{
    public static void Main()
    {
        Dictionary<int,string> dict_1 =new Dictionary<int, string>();
        dict_1.Add(1,"a");
        dict_1.Add(2,"b");
        dict_1.Add(3,"c");
        Dictionary<int, string> dict_2 =new Dictionary<int,string>(dict_1);
        dict_1.Remove(1);//affects dict_2 also which should not happen. 
        Console.WriteLine(dict_1.Count);
        Console.WriteLine(dict_2.Count);
    }
}





你可以在这里运行:

C#在线编译器| .NET小提琴 [ ^ ]



正如预期的那样,输出2后跟3,所以字典的内容显然不同。调试器将允许您观察字典的精确内容。



You can run it here:
C# Online Compiler | .NET Fiddle[^]

As expected, outputs 2 followed by 3, so clearly the content of the dictionaries differs. The debugger will allow you to observe the precise content of the dictionary.


我最近发布了一个合并两个字典的示例,其中,在重复值的情况下,您可以控制使用哪个值在合并的(新)字典中[ ^ ]



为了让dict2成为dict1的真正独立,你必须做 KeyValuePairs的副本,但是,如果Keys是整数,并且值字符串,这很容易,因为涉及的类型不需要实现ICloneable。



你可以这样做:
I recently posted an example of merging two Dictionaries where, in the case of duplicate Values, you can control which Value is used in the merged (new) Dictionary [^]

In order for dict2 to be truly independent of dict1, you must do a deep copy of the KeyValuePairs, however, if the Keys are integers, and the Values strings, this is easy, because the Types involved do not require implementing ICloneable.

You can just do this:
Dictionary<int, string> dict1 = new Dictionary<int, string>(dict2);

当断言从dict1中删除KeyValuePair会影响dict2时,你是不正确的。

You are incorrect when you assert removing the KeyValuePair from dict1 would affect dict2.


如果您希望仅更新已更改的项目而不是每次更新时都创建新副本,您可以执行以下操作。

If you wish to update only the items that have changed rather than make a new copy on every update, you could do something like this.

 var missingKeys = dictA.Where(p => !dictB.ContainsKey(p.Key));
 var missingValues = dictA.Where(p => dictB.ContainsKey(p.Key) && dictB[p.Key]!=p.Value);
foreach(var p in missingKeys)
 {
     dictB.Add(p.Key, p.Value);
 }
 foreach (var p in missingValues)
 {
     dictB[p.Key]=p.Value;
 }


这篇关于如何在C#中创建字典备份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆