Introduction
C#4.0 has introduce the new Zip extension method.
Purpose
Merges two sequences by using the specified predicate function.
Signature
public static IEnumerable Zip
(
this IEnumerable first
, IEnumerable second
, Func resultSelector
);
A word about the parameters
first: The first sequence to merge.
second: The second sequence to merge.
resultSelector: A function that specifies how to merge the elements from the two sequences.
Returns:An System.Collections.Generic.IEnumerable that contains merged elements of two input sequences.
Using the code
Let us see some examples
Sample input
List x = new List { 10, 20, 30, 40 };
List y = new List { 1, 2, 3, 4 };
Aim:
To perform x[i] * y[i]
Desired Output:
Approach 1: Traditional For Loop
int itemCount = x.Count;
for (int i = 0; i < itemCount; i++)
Console.WriteLine(x[i] * y[i]);
Console.ReadKey(true);
Explanation:
First we are keeping a count of the item (here it is 4). Next we are picking up the items at their respective position and just multiplying
Approach 2: Using Foreach loop
foreach(int i in x)
{
foreach (int j in y)
{
Console.WriteLine(i * j);
y.Remove(j);
break;
}
}
Explanation:
Here we are picking up the items from the x list in the outer foreach loop. Next in the inner loop we are picking up the items from the y list. Then multiplying the terms and removing the same from the y-list. Since the item indexes should match while performing the multiplication, henceforth we need the break statement.
Approach 3:Using the Extension methods of framework 3.5
Enumerable.Range(0, x.Count)
.Select(i => x[i] * y[i])
.ToList()
.ForEach(i=>Console.WriteLine(i));
Explanation:
The Enumerable.Range method generates a sequence of integral numbers within a specified range (here it is 0 to x.Count which is 4). Then projecting each element of a sequence into a new form using the Select method and using the Foreach extension method performing the display action on each element.
Approach 4: Using Zip Extension Method of Framework 4.0
x.Zip(y, (a, b) => a * b)
.ToList()
.ForEach(i => Console.WriteLine(i));
Explanation:
We are merging the two sequences by using the predicate function and using the Foreach extension method performing the display action on each element.
Another point of the extension method is that, it iterates by considering the least source.
E.g.
Let us take the source as either
List X = new List { 10, 20, 30 }; Element 40 has been removed
List Y = new List { 1, 2, 3, 4};
Or
List X = new List { 10, 20, 30 ,40 };
List Y = new List { 1, 2, 3}; Element 4 has been removed
For the first case between the two collections, the least number of item count is 3 (in X list) while the same item count holds good for case two but for list Y.
And method will iterate 3 times as depicted in the result
But the other approaches will fail giving the ArgumentOutOfRangeException which on the other hand needs special treatment in order to work properly.
References
Enumerable.Zip
Conclusion:
In this short tutorial we have seen some of the handy uses of the Zip extension method introduce in framework 4.0.
Comments on the topic are highly appreciated for the improvement of the topic.
Thanks for reading the article.