简体   繁体   English

对象寻址/访问问题中的C#列表

[英]C# list within object addressing/access issue

I've got some code. 我有一些代码。 I have a class: 我有一堂课:

public class TestClass
{
    private string String1 = "";
    private List<string> Strings = new List<string>();

    public TestClass(string String1, List<string> Strings)
    {
        this.String1 = String1;
        this.Strings = Strings;
    } // end constructor.

    // Associated get/set methods.
} // end class.

I then have (in another class, some code which uses this): 然后,我有(在另一个类中,一些使用此代码的代码):

public TestMethod()
{
    List<string> Strings = new List<string>();
    List<TestClass> MasterList = new List<TestClass>();
    int Counter = 0;
    string Name = " ... " // <- updated every time.

    while(Condition1)
    {
        if(Condition2)
        {
            Strings.Add(Counter.ToString());
        }
        else
        {
            MasterList.Add(new TestClass(Name, Strings));
            Name = // ... <- name updated here.
            Strings.Clear(); // Clear array.
        } // end if.
    } // end while.
} // end method.

The first time, the first element of MasterList is "Name1" and the list contains "1, 2, 3". 第一次, MasterList的第一个元素是“ Name1”,并且列表包含“ 1、2、3”。 The next time, the MasterList contains "Name2" and "4, 5, 6" but the first element now contains "4, 5, 6" and not "1, 2, 3". 下次, MasterList包含“ Name2”和“ 4、5、6”,但是第一个元素现在包含“ 4、5、6”而不是“ 1、2、3”。 When running for some time, the "Name1", "Name2" gets updated every time but every elements list is exactly the same contents, for example, what should be the output: 运行一段时间后,“ Name1”,“ Name2”每次都会更新,但是每个元素列表的内容都完全相同,例如,输出应为:

"Name1" -> "1, 2, 3" "Name2" -> "4, 5, 6" "Name3" -> "7, 8, 9" "Name1" -> "1, 2, 3" "Name2" -> "4, 5, 6" "Name3" -> "7, 8, 9" "Name2" -> "4, 5, 6" "Name3" -> "7, 8, 9"

What actually happens: 实际发生的情况:

"Name1" -> "7, 8, 9" "Name2" -> "7, 8, 9" "Name3" -> "7, 8, 9" "Name1" -> "7, 8, 9" "Name2" -> "7, 8, 9" "Name3" -> "7, 8, 9" "Name2" -> "7, 8, 9" "Name3" -> "7, 8, 9"

Trying to figure out what I've done wrong here, any ideas? 试图找出我在这里做错了什么,有什么想法吗? Is this some kind of reference issue? 这是某种参考问题吗?

Thanks! 谢谢! Jonathan 乔纳森

You are reusing the same list all the times: 您一直在重复使用同一列表:

MasterList.Add(new TestClass(Name, Strings)); //<<- Strings is always the same instance

So any change you do on the list will be propagated to all the child classes, a List is a reference object, so when you pass it to a function you don't pass a structure of data but a reference to the object, thus all the classes point to the same object. 因此,您在列表上所做的任何更改都会传播到所有子类,一个List是一个引用对象,因此当您将其传递给函数时,您不会传递数据结构而是传递对该对象的引用,因此所有这些类指向同一对象。

A very simple way of solving this is to replace: 解决此问题的一种非常简单的方法是替换:

Strings.Clear(); // Clear array.

with: 有:

Strings = new List<string>();

In this way you will pass the reference to a new instance each time. 这样,您每次都会将引用传递给新实例。

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

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