Assuming the source implements
IList<T>
or
IReadOnlyList<T>
:
for (int i = 0; i < list.Count - 1; i++)
{
var first = list[i];
var second = list[i + 1];
}
If you want to use LINQ:
foreach (var pair in list.Zip(list.Skip(1), (first, second) => (first, second)))
{
var first = pair.first;
var second = pair.second;
}
(Using the new C# 7 value tuple syntax[^]. If you're using an older compiler, or targeting an older framework, use an anonymous type instead.)