What you want to see on DotNetFunda.com ?
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 21043 |  Welcome, Guest!   Register  Login
 Home > Interview Questions > C# Interview Questions > What is the purpose of ZIP extension met ...

What is the purpose of ZIP extension method? Explain with example.

Interview question and answer by: Niladri.Biswas | Posted on: 7/11/2012 | Category: C# Interview questions | Views: 307 | | Points: 40


Answer:

The new ZIP extension method introduced in C#4.0 merges two collection sequences by using a predicate function.It is a Combining operator.It combines items from one collection with items from second that are in the same position in the collection.

If Collection A has N items and Collection B has N items (N = 1, 2, 3, 4...), the ZIP extension method will perform the operation as A[N] with B[N].


Let us see an example

Sample input

List<int> firstCollection = new List<int> { 11,12,13,14,15 };
List<int> secondCollection = new List<int> { 6,7,8,9,10 };


Aim:

To perform x + y

Solution using ZIP extension method



var firstCollection = new List<int> { 11,12,13,14,15 };
var secondCollection = new List<int> { 6,7,8,9,10 };

firstCollection
.Zip(secondCollection, (a, b) => a + b)
.ToList()
.ForEach(i => Console.WriteLine(i));



Result


17
19
21
23
25


Explanation:

We are merging the two sequences by using the predicate function ((a, b) => a + b) and using the Foreach extension method performing the display action on each element.

Asked In: Many Interviews | Alert Moderator 
Found interesting? Add this to:


>> Write Response - Respond to this post and get points

Even more ... | Submit Interview Questions and win prizes!

More Interview Questions from Niladri.Biswas

Even more ... | Submit Interview Questions and win prizes!


About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/23/2013 3:31:53 AM