If I define:
//class level declaration (in a console app)
static List<string> strings;
static void Loop(string s)
{
Console.WriteLine(s);
strings.Add(s + "!");
}
Then
static void Main(string[] args)
{
strings = new List<string> { "sample" };
strings.ForEach(s => Console.WriteLine(s));
}
executes normally, outputing a single string, while
static void Main(string[] args)
{
strings = new List<string> { "sample" };
strings.ForEach(s => Loop(s));
}
loops indefinitely, adding '!'s in the process, and
static void Main(string[] args)
{
strings = new List<string> { "sample" };
foreach (string s in strings)
{
Loop(s);
}
}
throws an InvalidOperationException (Collection was modified; enumeration operation may not execute), which, in my opinion is the correct behavior. Why the List.ForEach method allows the list to be changed by the action, I do not know, but would like to find out :)
AddSerie, and what does it do? - SWekoAddSerieperhaps add an entry toIndexSeries? - Joey