简体   繁体   English

具有更改变量的C#列表

[英]C# List with changing variable

I have a List containing an object like this: 我有一个包含这样的对象的列表:

List<unit> unitlist = new List<unit>();

With unit beeing initialized like this: 使用单位蜂鸣初始化如下:

 public class unit
    {
        public string[] records;
    }

I then use a variable to add into the list: 然后,我使用一个变量添加到列表中:

var temp = new unit();
temp.records = csv.GetFieldHeaders; // using lumenworks framework to read a csv
unitlist.Add(temp);

When I now override temp with a new item line from the csv, the entry in the list unitlist is also changed: 现在,当我使用来自csv的新项目行覆盖temp时,列表unitlist中的条目也会更改:

while (csv.ReadNextRecord())
{
    for (int i = 0; i < csv.Fieldcount; i++) 
    {
        // Read the csv entry in the temp variable
        temp.records[i] = csv[i];
    }
    // check for specific field, and write to list
    if (temp.records[8] == "Ja")
        unitlist.Add(temp);
}

When I now check unitlist, all the entries are the last read line from the csv, because they all are changed when the temp-variable changes. 现在,当我检查unitlist时,所有条目都是从csv读取的最后一行,因为当temp变量更改时,所有条目都被更改了。 Why is that the case? 为什么会这样? How can I separate the List unitlist from the variable temp? 如何将列表单元列表与变量temp分开?

Because you're using the same bucket to store things in. 因为您使用相同的存储桶来存储内容。

If you create temp every time, this should fix your problem 如果您每次都创建temp ,这应该可以解决您的问题

var header = new unit();
header.records = csv.GetFieldHeaders; 
unitlist.Add(header);

...

while (csv.ReadNextRecord())
{
    var temp = new unit();
    temp.records = new string[header.records.Length]

    for (int i = 0; i < csv.Fieldcount; i++) 
    {
        // Read the csv entry in the temp variable
        temp.records[i] = csv[i];
    }
    // check for specific field, and write to list
    if (temp.records[8] == "Ja")
        unitlist.Add(temp);
}

You need to create temp object in each iteration like below. 您需要在每次迭代中创建临时对象,如下所示。 Please try this and check. 请尝试并检查。

while (csv.ReadNextRecord())
{
    var temp = new unit();
    temp.records = csv.GetFieldHeaders;

    for (int i = 0; i < csv.Fieldcount; i++) 
    {
        // Read the csv entry in the temp variable
        temp.records[i] = csv[i];
    }
    // check for specific field, and write to list
    if (temp.records[8] == "Ja")
        unitlist.Add(temp);
}

When you create the temp variable, it is referencing a location in memory where your object data is allocated. 创建temp变量时,它引用内存中分配对象数据的位置。 When you add it to unitlist , a reference is added to the list that points to the same location in memory. 将其添加到unitlistunitlist引用添加到列表中,指向内存中的相同位置。

Now, when you change temp.records[i] , it is updated in that same memory location. 现在,当您更改temp.records[i] ,它将在相同的内存位置中进行更新 So, you end up with a list of items, all pointing to the same object in memory, containing the last records in the CSV file. 因此,您最终得到一个项目列表,所有项目都指向内存中的同一对象,其中包含CSV文件中的最后一条records

Simply adding a temp = new unit(); 只需添加一个temp = new unit(); at the beginning of your while loop will cause each iteration to allocation a new object, with a new memory location, and have temp reference it. while循环开始时,将导致每次迭代分配具有新内存位置的新对象,并temp引用它。

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

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