Is there a way to iterate over multiple collections of data with the same loop?
So basically could this two seperate steps
foreach (var x in array1)
{
DoSomething(x);
}
foreach (var x in array2)
{
DoSomething(x);
}
be combined into one?
Of course this is possible - thanks to LINQ:
foreach (var x in array1.Concat(array2))
{
DoSomething(x);
}
Note that since LINQ operates at the enumerator level, it will not allocate a new array to hold elements of array1 and array2. So, on top of being rather elegant, this solution is also space-efficient.