Click here to Skip to main content
12,627,064 members (36,353 online)
Click here to Skip to main content
Add your own
alternative version


4 bookmarked

Zip Extension method- Dotnet 4.0

, 27 Oct 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
This article will describe some of the functionalities of ZIP Extension Method


C#4.0 has introduce the new Zip extension method.


Merges two sequences by using the specified predicate function.


public</span />

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<t> that contains merged elements of two input sequences.

Using the code

Let us see some examples

Sample input

List<int /> x = new</span />


To perform x[i] * y[i]

Desired Output:


Approach 1: Traditional For Loop

int</span />


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</span />


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</span />


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) =></span />


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.


Let us take the source as either

List<int /> X = new</span />


List<int /> X = new</span />

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.




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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

You may also be interested in...


Comments and Discussions

GeneralMy vote of 1 Pin
Batzen29-Oct-10 8:15
memberBatzen29-Oct-10 8:15 
GeneralMy vote of 1 Pin
AxelM27-Oct-10 21:50
memberAxelM27-Oct-10 21:50 
GeneralMy vote of 1 Pin
SledgeHammer0127-Oct-10 8:24
memberSledgeHammer0127-Oct-10 8:24 
GeneralMy vote of 1 Pin
Toli Cuturicu27-Oct-10 3:54
memberToli Cuturicu27-Oct-10 3:54 
GeneralMy vote of 1 Pin
stefan popa27-Oct-10 3:17
memberstefan popa27-Oct-10 3:17 
GeneralMy vote of 1 Pin
Selvin26-Oct-10 5:56
memberSelvin26-Oct-10 5:56 
GeneralMy vote of 3 Pin
johannesnestler25-Oct-10 13:52
memberjohannesnestler25-Oct-10 13:52 
GeneralMy vote of 1 Pin
Argyle4Ever25-Oct-10 5:13
memberArgyle4Ever25-Oct-10 5:13 
General[My vote of 1] This is a tip at most.. Pin
Seishin#25-Oct-10 3:42
memberSeishin#25-Oct-10 3:42 
GeneralRe: [My vote of 1] This is a tip at most.. Pin
Niladri_Biswas25-Oct-10 5:04
memberNiladri_Biswas25-Oct-10 5:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.161205.3 | Last Updated 27 Oct 2010
Article Copyright 2010 by Niladri_Biswas
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid