具有可变变量的C#列表

具有可变变量的C#列表,c#,list,overriding,var,C#,List,Overriding,Var,我有一个包含如下对象的列表: List<unit> unitlist = new List<unit>(); public class unit { public string[] records; } 然后,我使用一个变量添加到列表中: var temp = new unit(); temp.records = csv.GetFieldHeaders; // using lumenworks framework to read a

我有一个包含如下对象的列表:

List<unit> unitlist = new List<unit>();
 public class unit
    {
        public string[] records;
    }
然后,我使用一个变量添加到列表中:

var temp = new unit();
temp.records = csv.GetFieldHeaders; // using lumenworks framework to read a csv
unitlist.Add(temp);
当我现在使用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);
}
while(csv.ReadNextRecord())
{
对于(int i=0;i

当我现在检查unitlist时,所有条目都是csv的最后一个读取行,因为当temp变量更改时,它们都会更改。为什么会这样?如何将列表unitlist与变量temp分开?

因为您使用的是同一个存储桶来存储内容

如果您每次都创建
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);
}
var头=新单位();
header.records=csv.GetFieldHeaders;
添加(表头);
...
而(csv.ReadNextRecord())
{
var temp=新装置();
temp.records=新字符串[header.records.Length]
对于(int i=0;i
因为你用的是同一个桶来存储东西

如果您每次都创建
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);
}
var头=新单位();
header.records=csv.GetFieldHeaders;
添加(表头);
...
而(csv.ReadNextRecord())
{
var temp=新装置();
temp.records=新字符串[header.records.Length]
对于(int i=0;i
您需要在每次迭代中创建临时对象,如下所示。请试试这个并检查一下

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);
}
while(csv.ReadNextRecord())
{
var temp=新装置();
temp.records=csv.GetFieldHeaders;
对于(int i=0;i
您需要在每次迭代中创建临时对象,如下所示。请试试这个并检查一下

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);
}
while(csv.ReadNextRecord())
{
var temp=新装置();
temp.records=csv.GetFieldHeaders;
对于(int i=0;i
创建
temp
变量时,它引用内存中分配对象数据的位置。将其添加到
单元列表
时,将向列表中添加一个指向内存中相同位置的引用

现在,当您更改
临时记录[i]
时,它会在相同的内存位置更新。因此,您将得到一个项目列表,所有项目都指向内存中的同一对象,其中包含CSV文件中最后的
记录


只需添加一个
temp=newunit()
循环开始时,而
循环将导致每次迭代分配一个新的对象和一个新的内存位置,并让
temp
引用它。

创建
temp
变量时,它引用的是内存中分配对象数据的位置。将其添加到
单元列表
时,将向列表中添加一个指向内存中相同位置的引用

现在,当您更改
临时记录[i]
时,它会在相同的内存位置更新。因此,您将得到一个项目列表,所有项目都指向内存中的同一对象,其中包含CSV文件中最后的
记录


只需添加一个
temp=newunit()while
循环开始时,code>将导致每次迭代分配一个新对象,该对象具有新的内存位置,并使
temp
引用它。

您需要创建新对象var temp=new unit();每次在循环中,然后将其添加到列表中;每次在循环中,然后将其添加到列表中。谢谢,这就解决了它。为什么temp.records=newstring[header.records.Length];必要吗?我想我已经用unit类的初始化初始化了字符串数组。@LucasEhrler,因为当你创建temp时,它有一个空数组tank you,这就解决了它。为什么temp.records=newstring[header.records.Length];必要吗?我想我已经用unit类的初始化初始化了字符串数组。@LucasEhrler因为当你创建temp时,它有一个空数组