Add elements to a list while iterating over it

Viewed 15531

I'm trying to add new elements to a list of lists while iterating over it

List<List<String>> sets = new List<List<string>>();

foreach (List<String> list in sets)
{
      foreach (String c in X)
      {
          List<String> newSet = ir_a(list, c, productions);

          if (newSet.Count > 0)
          {
              sets.Add(newSet);
          }
      }
}

The error I get after a few loops is this:

Collection was modified; enumeration operation may not execute

I know the error is caused by modifying the list, so my question is: What's the best or most fancy way to sort this thing out?

Thanks

1 Answers
Related