What's the best way to iterate over two collections of equal length?
Currently I'm using this:
for(var index = 0; index < collection1.Count(); index++)
{
var item1 = collection1[index];
var item2 = collection2[index];
Console.WriteLine(item1.ToString() + ", "+ item2.ToString());
}
But I'd love to be able to use a foreach
instead, e.g.
foreach(item1, item2 in collection1, collection2)
{
Console.WriteLine(item1.ToString() + ", " + item2.ToString();
}
Now, this would be sort of possible if I zipped my two collections into a dictionary, but is that the best way to go? Would that be a sufficient gain of readability while sacrificing the performance needed to build the dictionary?
Similarly, I'm not sure if a dictionary is idealistically correct here, as there may not necessarily be a link between the two collections (other than their order in the list), merely a guarantee that they both have the same length.