Click here to Skip to main content
Licence CPOL
First Posted 25 Oct 2010
Views 5,764
Downloads 39
Bookmarked 3 times

Zip Extension method- Dotnet 4.0

By | 26 Oct 2010 | Article
This article will describe some of the functionalities of ZIP Extension Method

Introduction

C#4.0 has introduce the new Zip extension method.

Purpose

Merges two sequences by using the specified predicate function.

Signature

public</span /> static</span /> IEnumerable<tresult /> Zip<tfirst, />
(
	this</span /> IEnumerable<tfirst /> first
	, IEnumerable<tsecond /> second
	, Func<tfirst, /> 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<int /> x = new</span /> List<int /> { 10</span />, 20</span />, 30</span />, 40</span /> };
List<int /> y = new</span /> List<int /> { 1</span />, 2</span />, 3</span />, 4</span /> };

Aim:

To perform x[i] * y[i]

Desired Output:

1.jpg

Approach 1: Traditional For Loop

int</span /> itemCount = x.Count;
for</span /> (int</span /> i = 0</span />; i <</span /> itemCount; i++)            
Console.WriteLine(x[i] * y[i]);
Console.ReadKey(true</span />);

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</span /> i in</span /> x)
{
   foreach (int</span /> j in</span /> y)
   {
      Console.WriteLine(i * j);
      y.Remove(j);
      break</span />;
   }
}

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</span />, x.Count)
          .Select(i =></span /> x[i] * y[i])
          .ToList()
          .ForEach(i=></span />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) =></span /> a * b)
  .ToList()
  .ForEach(i =></span /> 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<int /> X = new</span /> List<int /> { 10</span />, 20</span />, 30</span /> }; //</span />Element 40 has been removed
</span />List<int /> Y = new</span /> List<int /> { 1</span />, 2</span />, 3</span />, 4</span />};

Or

List<int /> X = new</span /> List<int /> { 10</span />, 20</span />, 30</span /> ,40</span /> };
List<int /> Y = new</span /> List<int /> { 1</span />, 2</span />, 3</span />}; //</span /> Element 4 has been removed
</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

2.jpg

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.

License

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

About the Author

Niladri_Biswas

Software Developer (Senior)
Software industry
India India

Member

Lead Engineer at HCL Technologies Ltd.
Code Project MVP 2012

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 PinmemberBatzen7:15 29 Oct '10  
GeneralMy vote of 1 PinmemberAxelM20:50 27 Oct '10  
GeneralMy vote of 1 PinmemberSledgeHammer017:24 27 Oct '10  
GeneralMy vote of 1 PinmemberToli Cuturicu2:54 27 Oct '10  
GeneralMy vote of 1 Pinmemberstefan popa2:17 27 Oct '10  
GeneralMy vote of 1 PinmemberSelvin4:56 26 Oct '10  
GeneralMy vote of 3 Pinmemberjohannesnestler12:52 25 Oct '10  
GeneralMy vote of 1 PinmemberArgyle4Ever4:13 25 Oct '10  
General[My vote of 1] This is a tip at most.. PinmemberSeishin#2:42 25 Oct '10  
GeneralRe: [My vote of 1] This is a tip at most.. PinmemberNiladri_Biswas4:04 25 Oct '10  

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

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

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