C# 字典添加到错误的键/值对

发布于 2024-10-24 20:17:33 字数 1277 浏览 2 评论 0原文

我的程序中有以下 foreach 循环:

Dictionary<string, int[]> summaryDate_clipsEpisodesImps = new Dictionary<string, int[]>();
Dictionary<string, int[]> summaryPartner_clipsEpisodesImps = new Dictionary<string, int[]>();    

foreach (DataRow row1 in dt10.Rows)
{
     int[] numbers = new int[3];
     numbers[0] = 0;
     numbers[1] = 0;
     numbers[2] = 0;

     if (!dictionary1.ContainsKey(row1["value1"].ToString().Trim()))
     {
          dictionary1.Add(row1["value1"].ToString().Trim(), numbers);
     }

     if (!dictionary2.ContainsKey(row1["value2"].ToString().Trim()))
     {
          dictionary2.Add(row1["value2"].ToString().Trim(), numbers);
     }

     if (row1["yes_or_no"].ToString().Trim() == "yes")
     {
          dictionary1[row1["value1"].ToString().Trim()][0] = dictionary1[row1["value1"].ToString().Trim()][0] + Convert.ToInt32(row1["a_number"]);
          dictionary2[row1["value2"].ToString().Trim()][0] = dictionary2[row1["value2"].ToString().Trim()][0] + Convert.ToInt32(row1["a_number"]);
     }
}

本质上,我循环访问数据表并根据在每个记录中找到的值创建字符串/int 数组的字典。然后,我尝试根据记录中另一个字段的存在来增加数组中的第一个值。

当我检查 yes_or_no 值时,我的问题出现在 if 语句中。当该语句中出现第二行递增字典 2 时,字典 1 中的值将递增相同的值。我不知道为什么会这样。

如果这还不完全清楚,请告诉我。预先感谢您的帮助。

I have the following foreach loop in my program:

Dictionary<string, int[]> summaryDate_clipsEpisodesImps = new Dictionary<string, int[]>();
Dictionary<string, int[]> summaryPartner_clipsEpisodesImps = new Dictionary<string, int[]>();    

foreach (DataRow row1 in dt10.Rows)
{
     int[] numbers = new int[3];
     numbers[0] = 0;
     numbers[1] = 0;
     numbers[2] = 0;

     if (!dictionary1.ContainsKey(row1["value1"].ToString().Trim()))
     {
          dictionary1.Add(row1["value1"].ToString().Trim(), numbers);
     }

     if (!dictionary2.ContainsKey(row1["value2"].ToString().Trim()))
     {
          dictionary2.Add(row1["value2"].ToString().Trim(), numbers);
     }

     if (row1["yes_or_no"].ToString().Trim() == "yes")
     {
          dictionary1[row1["value1"].ToString().Trim()][0] = dictionary1[row1["value1"].ToString().Trim()][0] + Convert.ToInt32(row1["a_number"]);
          dictionary2[row1["value2"].ToString().Trim()][0] = dictionary2[row1["value2"].ToString().Trim()][0] + Convert.ToInt32(row1["a_number"]);
     }
}

Essentially, I am looping through a datatable and creating a dictionary of string/int arrays based on values that I find in each record. Then I'm trying to increment the first value in the array based on the presence of another field in the record.

My problem occurs in the if statement when I check for the yes_or_no value. When the second line incrementing dictionary2 is present in this statement, the value in dictionary 1 is incremented by the same value. I have no idea why this is the case.

Please let me know if this isn't entirely clear. Thanks in advance for the help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一场春暖 2024-10-31 20:17:33

这是因为两个字典条目中的值都指向 numbers 的同一个实例。这意味着其中一项的任何更改都会影响另一项。

要在每个字典中添加一个新实例,您可以执行类似以下操作:

...
if (!dictionary1.ContainsKey(row1["value1"].ToString().Trim()))
{
    dictionary1.Add(row1["value1"].ToString().Trim(), GetNumbersArray());
}
if (!dictionary2.ContainsKey(row1["value2"].ToString().Trim()))
{
     dictionary2.Add(row1["value2"].ToString().Trim(), GetNumbersArray());
}
...

private int[] GetNumbersArray()
{
    int[] numbers = new int[3];

    numbers[0] = 0;
    numbers[1] = 0;
    numbers[2] = 0;

    return numbers;
}

This is because the values in both dictionary entries point to the same instance of numbers. This means any change to one will affect the other.

To put a new instance in each dictionary you could do something similar to:

...
if (!dictionary1.ContainsKey(row1["value1"].ToString().Trim()))
{
    dictionary1.Add(row1["value1"].ToString().Trim(), GetNumbersArray());
}
if (!dictionary2.ContainsKey(row1["value2"].ToString().Trim()))
{
     dictionary2.Add(row1["value2"].ToString().Trim(), GetNumbersArray());
}
...

private int[] GetNumbersArray()
{
    int[] numbers = new int[3];

    numbers[0] = 0;
    numbers[1] = 0;
    numbers[2] = 0;

    return numbers;
}
薄荷梦 2024-10-31 20:17:33

您正在向每个字典添加一个指向数组的指针。两个字典都查看相同的数组,因此当您通过dictionary2修改数组时,dictionary1会看到更改。

You're adding a pointer to the array to each dictionary. Both dictionaries look at the same array, so when you modify the array through dictionary2, dictionary1 sees the changes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文