Click here to Skip to main content
15,867,686 members
Articles / Desktop Programming / Win32

Zip Extension method- Dotnet 4.0

Rate me:
Please Sign up or sign in to vote.
2.63/5 (16 votes)
26 Oct 2010CPOL2 min read 23.3K   113   4   10
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<t> 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)



Comments and Discussions

 
GeneralMy vote of 1 Pin
Batzen29-Oct-10 7:15
Batzen29-Oct-10 7:15 
GeneralMy vote of 1 Pin
AxelM27-Oct-10 20:50
AxelM27-Oct-10 20:50 
GeneralMy vote of 1 Pin
SledgeHammer0127-Oct-10 7:24
SledgeHammer0127-Oct-10 7:24 
GeneralMy vote of 1 Pin
Toli Cuturicu27-Oct-10 2:54
Toli Cuturicu27-Oct-10 2:54 
GeneralMy vote of 1 Pin
stefan popa27-Oct-10 2:17
stefan popa27-Oct-10 2:17 
GeneralMy vote of 1 Pin
Selvin26-Oct-10 4:56
Selvin26-Oct-10 4:56 
GeneralMy vote of 3 Pin
johannesnestler25-Oct-10 12:52
johannesnestler25-Oct-10 12:52 
GeneralMy vote of 1 Pin
Argyle4Ever25-Oct-10 4:13
Argyle4Ever25-Oct-10 4:13 
General[My vote of 1] This is a tip at most.. Pin
Seishin#25-Oct-10 2:42
Seishin#25-Oct-10 2:42 
GeneralRe: [My vote of 1] This is a tip at most.. Pin
Niladri_Biswas25-Oct-10 4:04
Niladri_Biswas25-Oct-10 4: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.