How to add items to a collection while consuming it?

后端 未结 11 674
离开以前
离开以前 2021-01-15 12:44

The example below throws an InvalidOperationException, \"Collection was modified; enumeration operation may not execute.\" when executing the code.

var urls         


        
11条回答
  •  无人及你
    2021-01-15 12:57

    Jon's approach is right; a queue's the right data structure for this kind of application.

    Assuming that you'd eventually like your program to terminate, I'd suggest two other things:

    • don't use string for your URLs, use System.Web.Uri: it provides a canonical string representation of the URL. This will be useful for the second suggestion, which is...
    • put the canonical string representation of each URL you process in a Dictionary. Before you enqueue a URL, check to see if it's in the Dictionary first.

提交回复
热议问题