C# Enumerable Zip
Description
Applies a specified function to the corresponding elements of two sequences, producing a sequence of the results.
Syntax
public static IEnumerable<TResult> Zip<TFirst, TSecond, TResult>(
this IEnumerable<TFirst> first,
IEnumerable<TSecond> second,/*from ww w.j ava 2s . c o m*/
Func<TFirst, TSecond, TResult> resultSelector
)
Parameters
TFirst
- The type of the elements of the first input sequence.TSecond
- The type of the elements of the second input sequence.TResult
- The type of the elements of the result sequence.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.
Example
The following code example demonstrates how to use the Zip method to merge two sequences.
//from ww w.j a v a 2s. c o m
using System;
using System.Linq;
using System.Collections.Generic;
public class MainClass{
public static void Main(String[] argv){
int[] numbers = { 1, 2, 3, 4 };
string[] words = { "one", "two", "three" };
var numbersAndWords = numbers.Zip(words, (first, second) => first + " " + second);
foreach (var item in numbersAndWords)
Console.WriteLine(item);
}
}
The code above generates the following result.