Skip to main content
EnumerableZip Method
 

Applies a specified function to the corresponding elements of two sequences, producing a sequence of the results.

Namespace:   System.Linq
Assembly:  System.Core (in System.Core.dll)

Syntax
public static IEnumerable<TResult> Zip<TFirst, TSecond, TResult>(
	this IEnumerable<TFirst> first,
	IEnumerable<TSecond> second,
	Func<TFirst, TSecond, TResult> resultSelector
)
public:
generic<typename TFirst, typename TSecond, typename TResult>
[ExtensionAttribute]
static IEnumerable<TResult>^ Zip(
	IEnumerable<TFirst>^ first,
	IEnumerable<TSecond>^ second,
	Func<TFirst, TSecond, TResult>^ resultSelector
)
static member Zip<'TFirst, 'TSecond, 'TResult> : 
        first:IEnumerable<'TFirst> *
        second:IEnumerable<'TSecond> *
        resultSelector:Func<'TFirst, 'TSecond, 'TResult> -> IEnumerable<'TResult>
<ExtensionAttribute>
Public Shared Function Zip(Of TFirst, TSecond, TResult) (
	first As IEnumerable(Of TFirst),
	second As IEnumerable(Of TSecond),
	resultSelector As Func(Of TFirst, TSecond, TResult)
) As IEnumerable(Of TResult)

Parameters

first
Type:

The first sequence to merge.

second
Type:

The second sequence to merge.

resultSelector
Type:

A function that specifies how to merge the elements from the two sequences.

Return Value

Type:

An IEnumerable that contains merged elements of two input sequences.

Type 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.

Exceptions
Exception Condition
ArgumentNullException

first or second is null.

Remarks

This method is implemented by using deferred execution. The immediate return value is an object that stores all the information that is required to perform the action. The query represented by this method is not executed until the object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or For Each in Visual Basic.

The method merges each element of the first sequence with an element that has the same index in the second sequence. If the sequences do not have the same number of elements, the method merges sequences until it reaches the end of one of them. For example, if one sequence has three elements and the other one has four, the result sequence will have only three elements.

Examples

The following code example demonstrates how to use the Zip method to merge two sequences.

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);

// This code produces the following output:

// 1 one
// 2 two
// 3 three
Dim numbers() As Integer = {1, 2, 3, 4}
Dim words() As String = {"one", "two", "three"}
Dim numbersAndWords = numbers.Zip(words, Function(first, second) first & " " & second)

For Each item In numbersAndWords
    Console.WriteLine(item)
Next

' This code produces the following output:

' 1 one
' 2 two
' 3 three

Version Information
Universal Windows Platform
Available since 8
.NET Framework
Available since 4.0
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 4.0
Windows Phone Silverlight
Available since 8.0
Windows Phone
Available since 8.1
Return to top