下面的示例抛出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)
我怎样才能以更好的方式重写这个?我猜一个递归的解决方案?