具有变化变量的 C# 列表
C# List with changing variable
我有一个包含如下对象的列表:
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);
}
当我现在检查 unitlist 时,所有条目都是从 csv 中读取的最后一行,因为当临时变量更改时它们都会更改。为什么会这样?如何将 List 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);
}
您需要在每次迭代中创建临时对象,如下所示。请尝试并检查。
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);
}
当您创建 temp
变量时,它引用内存中分配对象数据的位置。当您将它添加到 unitlist
时,一个引用将添加到指向内存中相同位置的列表。
现在,当您更改 temp.records[i]
时,它会在同一内存位置 更新 。因此,您最终得到一个项目列表,所有项目都指向内存中的同一个对象,包含 CSV 文件中的最后一个 records
。
简单地在 while
循环的开头添加一个 temp = new unit();
将导致每次迭代分配一个新对象,具有新的内存位置,并让 temp
引用它。
我有一个包含如下对象的列表:
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);
}
当我现在检查 unitlist 时,所有条目都是从 csv 中读取的最后一行,因为当临时变量更改时它们都会更改。为什么会这样?如何将 List 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);
}
您需要在每次迭代中创建临时对象,如下所示。请尝试并检查。
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);
}
当您创建 temp
变量时,它引用内存中分配对象数据的位置。当您将它添加到 unitlist
时,一个引用将添加到指向内存中相同位置的列表。
现在,当您更改 temp.records[i]
时,它会在同一内存位置 更新 。因此,您最终得到一个项目列表,所有项目都指向内存中的同一个对象,包含 CSV 文件中的最后一个 records
。
简单地在 while
循环的开头添加一个 temp = new unit();
将导致每次迭代分配一个新对象,具有新的内存位置,并让 temp
引用它。