gpt4 book ai didi

c# 修改 List 中的结构

转载 作者:可可西里 更新时间:2023-11-01 07:44:39 28 4
gpt4 key购买 nike

简短的问题:如何修改 List 中的单个项目? (或者更准确地说,struct 的成员存储在 List 中?)

完整解释:

首先,下面使用的struct定义:

public struct itemInfo
{
...(Strings, Chars, boring)...
public String nameStr;
...(you get the idea, nothing fancy)...
public String subNum; //BTW this is the element I'm trying to sort on
}

public struct slotInfo
{
public Char catID;
public String sortName;
public Bitmap mainIcon;
public IList<itemInfo> subItems;
}

public struct catInfo
{
public Char catID;
public String catDesc;
public IList<slotInfo> items;
public int numItems;
}

catInfo[] gAllCats = new catInfo[31];

gAllCats 在加载时填充,在程序运行时以此类推。

当我想对 subItems 数组中的 itemInfo 对象进行排序时,问题就出现了。我正在使用 LINQ 来执行此操作(因为似乎没有任何其他合理的方法来对非内置类型的列表进行排序)。所以这就是我所拥有的:

foreach (slotInfo sInf in gAllCats[c].items)
{
var sortedSubItems =
from itemInfo iInf in sInf.subItems
orderby iInf.subNum ascending
select iInf;
IList<itemInfo> sortedSubTemp = new List<itemInfo();
foreach (itemInfo iInf in sortedSubItems)
{
sortedSubTemp.Add(iInf);
}
sInf.subItems.Clear();
sInf.subItems = sortedSubTemp; // ERROR: see below
}

错误是“无法修改‘sInf’的成员,因为它是‘foreach 迭代变量’”。

a,这个限制没有意义;这不是 foreach 结构的主要用途吗?

b,(同样出于恶意)如果不修改列表,Clear() 会做什么? (顺便说一句,根据调试器,如果我删除最后一行并运行它,列表确实会被清除。)

所以我尝试采用不同的方法,看看它是否可以使用常规的 for 循环。 (显然,这只是允许的,因为 gAllCats[c].items 实际上是一个 IList;我不认为它会让你索引一个常规的 List 这样。)

for (int s = 0; s < gAllCats[c].items.Count; s++)
{
var sortedSubItems =
from itemInfo iInf in gAllCats[c].items[s].subItems
orderby iInf.subNum ascending
select iInf;
IList<itemInfo> sortedSubTemp = new List<itemInfo>();
foreach (itemInfo iInf in sortedSubItems)
{
sortedSubTemp.Add(iInf);
}
//NOTE: the following two lines were incorrect in the original post
gAllCats[c].items[s].subItems.Clear();
gAllCats[c].items[s].subItems = sortedSubTemp; // ERROR: see below
}

这一次,错误是“无法修改‘System.Collections.Generic.IList.this[int]’的返回值,因为它不是变量。”啊!如果不是变量,它是什么?它什么时候变成了“返回值”?

我知道必须有一种“正确”的方式来做到这一点;我是从 C 背景而来的,我知道我可以在 C 中做到这一点(尽管有很多手动内存管理。)

我四处搜索,似乎 ArrayList 已经过时了,取而代之的是泛型(我使用的是 3.0)而且我不能使用数组,因为大小需要动态的。

最佳答案

查看 for 循环方法,原因(和解决方案)在 documentation for the compilation error 中给出:

An attempt was made to modify a value type that is produced as the result of an intermediate expression but is not stored in a variable. This error can occur when you attempt to directly modify a struct in a generic collection.

To modify the struct, first assign it to a local variable, modify the variable, then assign the variable back to the item in the collection.

因此,在您的 for 循环中,更改以下行:

catSlots[s].subItems.Clear();
catSlots[s].subItems = sortedSubTemp; // ERROR: see below

...进入:

slotInfo tempSlot = gAllCats[0].items[s];
tempSlot.subItems = sortedSubTemp;
gAllCats[0].items[s] = tempSlot;

我删除了对 Clear 方法的调用,因为我认为它不会添加任何内容。

关于c# 修改 List<T> 中的结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1067340/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com