Click here to Skip to main content
11,479,034 members (61,565 online)
Click here to Skip to main content

Zip Extension method- Dotnet 4.0

, 27 Oct 2010 CPOL 11.5K 81 4
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author


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    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.150520.1 | Last Updated 27 Oct 2010
Article Copyright 2010 by Niladri_Biswas
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid