> 意外行为,c#,list,dictionary"/>

首页 > 解决方案 > 字典> 意外行为

问题描述

问题摘要。在我的程序中,我在字典中创建 5 个键,每个字典键从列表中获取值。最后,我将 5 个键的内容写入一个文件。如果我使用列表,则将值写入列表中,将列表放入 key1,清除列表以将其重用于 key2 等等,最后在文件中所有 5 个键与最后一个键具有相同的值. 但是,如果我为 5 个键中的每一个使用一个新列表,那么我会在文件中得到我期望得到的东西。

现在是代码。

List<string> tempList = new List<string>();

tempList.Clear(); foreach (string item in PSD.labadvancedPathS) { tempList.Add(item); }
Debug.WriteLine(String.Join("\r\n", tempList));
_iniFile.SetValueMultiLine("labadvancedPathS", tempList, false, true);

tempList.Clear(); foreach (string item in PSD.xmlCleanupBoolString) { tempList.Add(item); }
Debug.WriteLine(String.Join("\r\n", tempList));
_iniFile.SetValueMultiLine("xmlCleanupBoolString", tempList, false, true);

tempList.Clear(); foreach (string item in PSD.rmiPathS) { tempList.Add(item); }
Debug.WriteLine(String.Join("\r\n", tempList));
_iniFile.SetValueMultiLine("rmiPathS", tempList, false, true);

tempList.Clear(); foreach (string item in PSD.txtCleanupBoolString) { tempList.Add(item); }
Debug.WriteLine(String.Join("\r\n", tempList));
_iniFile.SetValueMultiLine("txtCleanupBoolString", tempList, false, true);

tempList.Clear(); foreach (string item in PSD.xmlPathS) { tempList.Add(item); }
Debug.WriteLine(String.Join("\r\n", tempList));
_iniFile.SetValueMultiLine("xmlPathS", tempList, false, true);

tempList.Clear(); foreach (string item in PSD.backupJobs) { tempList.Add(item); }
Debug.WriteLine(String.Join("\r\n", tempList));
_iniFile.SetValueMultiLine("backupJobs", tempList, false, true);
.
.
.
_iniFile.UpdateIniFile(true);

注意:在每个 Debug.WriteLine() 我都从 tempList 获得预期的输出

现在是 _iniFileSetValueMultiLine() 部分。

public class IniFile
    {
        private Dictionary<string, List<String>> settingsMultiLine = new Dictionary<string, List<String>>();

        public void SetValueMultiLine(string settingName, List<String> settingValue, bool updateTheIniFile = false, bool debugMessages = false)
        {
            if (settingsMultiLine.ContainsKey(settingName))
            {
                this.settingsMultiLine[settingName] = settingValue;
                if (debugMessages)
                {
                    MessageBox.Show("Found the setting " + settingName + " and putting in " + string.Join(", ", settingValue));
                }
            }
            else
            {
                settingsMultiLine.Add(settingName, settingValue);
                if (debugMessages)
                {
                    MessageBox.Show("Creating the setting " + settingName + " and putting in " + string.Join(", ", settingValue));
                }
                if (!messageWasShown)
                {
                    showErrorMessage(settingName + " was not found. Creating the setting " + settingName + " and putting in " + string.Join(", ", settingValue));
                }
            }
            if (updateTheIniFile) { this.UpdateIniFile(); }
        }

    }

注意 2:在 Messagebox.show() 中,我再次从设置名称和值中获得了预期的输出

最后,我写入文件的部分:

        public void UpdateIniFile(bool debugMessages = false)
        {
            List<string> whatToWriteToFile = new List<string>();
            foreach (string item in settingsMultiLine.Keys)//Enumerate through the multilined Settings
            {
                whatToWriteToFile.Add("[" + item + " START]");
                if (debugMessages) { MessageBox.Show("[" + item + " START]"); }
                foreach (string subItem in settingsMultiLine[item])//Enumerate through the values of the multilined setting
                {
                    whatToWriteToFile.Add(subItem);
                    if (debugMessages) { MessageBox.Show(subItem); }
                }
                whatToWriteToFile.Add("[" + item + " END]");
                if (debugMessages) { MessageBox.Show("[" + item + " END]"); }
                whatToWriteToFile.Add("");
            }
            foreach (string item in settingsSingleLine.Keys)
            {
                whatToWriteToFile.Add("[" + item + "]");
                whatToWriteToFile.Add(this.settingsSingleLine[item]);
                whatToWriteToFile.Add("");
            }

            File.WriteAllLines(iniFileFullPath, whatToWriteToFile);
        }

这就是“某事”发生的地方,文件中的 5 个键与最后一个键相同,消息框也显示“错误”信息。

感谢您阅读我,请对我温柔:)

标签: c#listdictionary

解决方案


这实际上是预期的行为。正如其他人在评论中所说,列表是一种引用类型,这意味着您并没有真正将列表添加到字典中,而是指向该列表的指针。指向该列表的所有实体都将观察到您对列表所做的任何更改。

作为参考,我建议查看参考类型与值类型,以获得更深入的理解。


推荐阅读