我正在遍历一个元素列表,并想为每个元素在 Collection 中的位置分配一个数字以用于删除目的。我下面的代码只是给了我计数,是否有另一种选择来实现这一点。例如。
0 只猫 1只狗 2条鱼
等..
foreach (string x in localList)
{
{
Console.WriteLine( localList.Count + " " + x);
}
}
请您参考如下方法:
老派,回到标准的 for 循环:
for(int i = 0; i < localList.Count; ++i)
{
string x = localList[i];
// i is the index of x
Console.WriteLine(i + " " + x);
}
