在 C# 中使用 foreach 循环时修改 Collection
基本上,我想在 foreach 循环内从列表中删除一个项目。 我知道使用 for 循环时这是可能的,但出于其他目的,我想知道使用 foreach 循环是否可以实现这一点。
在 python 中,我们可以通过执行以下操作来实现此目的:
a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
for i in a:
print i
if i == 1:
a.pop(1)
这给出了以下输出
>>>1
3
4
5
6
7
8
9
但是,当在 c# 中执行类似操作时,我得到一个 InvalidOperationException,我想知道是否有一种方法可以解决这个问题,而不仅仅是简单地使用for 循环。
我在抛出异常时使用的c#代码:
static void Main(string[] args)
{
List<string> MyList = new List<string>(new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9"});
foreach (string Item in MyList)
{
if (MyList.IndexOf(Item) == 0)
{
MyList.RemoveAt(1);
}
Console.WriteLine(Item);
}
}
提前致谢
Basically, I would like to remove an item from a list whilst inside the foreach loop. I know that this is possible when using a for loop, but for other purposes, I would like to know if this is achievable using a foreach loop.
In python we can achieve this by doing the following:
a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
for i in a:
print i
if i == 1:
a.pop(1)
This gives the following Output
>>>1
3
4
5
6
7
8
9
But when doing something similar in c#, I get an InvalidOperationException, I was wondering if there was a way of getting around this, without just simply using a for loop.
The code in c# that I used when the exception was thrown:
static void Main(string[] args)
{
List<string> MyList = new List<string>(new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9"});
foreach (string Item in MyList)
{
if (MyList.IndexOf(Item) == 0)
{
MyList.RemoveAt(1);
}
Console.WriteLine(Item);
}
}
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你不能这样做。 来自
IEnumerator
的文档:替代方法是:
最后一个替代方案是 LINQ-就像解决方案一样,您通常会在其中编写:(
当然,其中
ShouldBeRetained
是您想要的任何逻辑。)仅当您确实想要时,才需要调用ToList()
它在一个列表中。 这会产生更具声明性的代码,通常更容易阅读。 我无法轻易猜测您原来的循环的用途(目前看起来很奇怪),而如果您可以纯粹用项目来表达逻辑,那么它会更清晰。You can't do this. From the docs for
IEnumerator<T>
:Alternatives are:
The last of these alternatives is the LINQ-like solution, where you'd typically write:
(Where
ShouldBeRetained
is whatever logic you want, of course.) The call toToList()
is only necessary if you actually want it in a list. This leads to more declarative code which is often easier to read. I can't easily guess what your original loop is meant to do (it seems pretty odd at the moment) whereas if you can express the logic purely in terms of the item, it can be a lot clearer.如果您需要的只是删除满足条件的所有项目,您可以使用 List< T>.RemoveAll 方法:
请注意,这会修改初始列表。
If all you need is to remove all items that satisfy a condition you could use the List<T>.RemoveAll method:
Note that this modifies the initial list.
在对集合使用 foreach 循环时,绝对不能以任何方式更改集合。
您可以使用 for 循环并自行管理索引,或者制作集合的副本,并在循环原始集合时,从副本中删除与原始集合中的项目相同的项目。
在这两种情况下,它都不是那么清晰或方便:)。
You definitely may not change a collection in any way when using a foreach loop on it.
You can use a for loop and manage the index for yourself or make a copy of the collection and as you are looping the original, remove items from the copy that equal the item in the original.
In both cases it's not quite as clear or convenient :).