繁体   English   中英

字典<string, List<String> &gt; 意外行为

[英]Dictionary<string, List<String>> unexpected behavior

问题摘要。 在我的程序中,我在字典中创建了 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 个键与最后一个相同,并且消息框显示“错误”信息。

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM