Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to iterate two Lists or Arrays with one foreach statement in C#?
When working with multiple collections in C#, you often need to iterate through them simultaneously. The Zip() method from LINQ provides an elegant solution to combine two arrays or lists and iterate them with a single foreach statement.
Syntax
Following is the syntax for using the Zip() method to combine two collections −
var result = collection1.Zip(collection2, (item1, item2) => new { Prop1 = item1, Prop2 = item2 });
The lambda expression defines how to combine elements from both collections into a new anonymous object.
Using Zip() with Arrays
The Zip() method processes two collections element by element, creating pairs from corresponding positions −
using System;
using System.Linq;
public class Demo {
public static void Main() {
var numbers = new[] { 20, 40, 60 };
var elements = new[] { "ele1", "ele2", "ele3" };
var combined = numbers.Zip(elements, (n, e) => new { Number = n, Element = e });
foreach(var item in combined) {
Console.WriteLine(item.Number + " -> " + item.Element);
}
}
}
The output of the above code is −
20 -> ele1 40 -> ele2 60 -> ele3
Using Zip() with Lists
The same approach works with List<T> collections −
using System;
using System.Collections.Generic;
using System.Linq;
public class ListExample {
public static void Main() {
var names = new List<string> { "Alice", "Bob", "Charlie" };
var ages = new List<int> { 25, 30, 35 };
var people = names.Zip(ages, (name, age) => new { Name = name, Age = age });
foreach(var person in people) {
Console.WriteLine($"{person.Name} is {person.Age} years old");
}
}
}
The output of the above code is −
Alice is 25 years old Bob is 30 years old Charlie is 35 years old
Using Zip() with Different Collection Types
You can combine different collection types, such as an array with a list −
using System;
using System.Collections.Generic;
using System.Linq;
public class MixedExample {
public static void Main() {
var prices = new[] { 10.99, 25.50, 5.75 };
var products = new List<string> { "Apple", "Book", "Pen" };
var catalog = products.Zip(prices, (product, price) => $"{product}: ${price}");
foreach(var item in catalog) {
Console.WriteLine(item);
}
}
}
The output of the above code is −
Apple: $10.99 Book: $25.5 Pen: $5.75
How It Works
The Zip() method works by pairing elements at corresponding indices from both collections. If collections have different lengths, the iteration stops when the shorter collection is exhausted.
Key Rules
-
Both collections must implement
IEnumerable<T>. -
Iteration stops when the shorter collection is exhausted.
-
The lambda function defines how to combine corresponding elements.
-
Requires
using System.Linq;directive.
Conclusion
The Zip() method provides an elegant way to iterate through multiple collections simultaneously using a single foreach loop. It combines corresponding elements from each collection based on their index positions, making parallel iteration simple and readable.
