相关疑难解决方法(0)

如何在消费时将项目添加到集合中?

下面的示例抛出InvalidOperationException,"Collection已被修改;枚举操作可能无法执行".执行代码时.

var urls = new List<string>();
urls.Add("http://www.google.com");

foreach (string url in urls)
{
    // Get all links from the url
    List<string> newUrls = GetLinks(url);

    urls.AddRange(newUrls); // <-- This is really the problematic row, adding values to the collection I'm looping
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能以更好的方式重写这个?我猜一个递归的解决方案?

.net c#

2
推荐指数
1
解决办法
1161
查看次数

标签 统计

.net ×1

c# ×1