Enumerable.Zip 方法

定義

多載

Zip<TFirst,TSecond,TResult>(IEnumerable<TFirst>, IEnumerable<TSecond>, Func<TFirst,TSecond,TResult>)

將指定的函式套用至兩個序列的對應項目,產生結果的序列。

Zip<TFirst,TSecond,TThird>(IEnumerable<TFirst>, IEnumerable<TSecond>, IEnumerable<TThird>)

產生具有來自三個指定序列之元素的元組序列。

Zip<TFirst,TSecond>(IEnumerable<TFirst>, IEnumerable<TSecond>)

從兩個指定序列中的元素產生一系列元組。

Zip<TFirst,TSecond,TResult>(IEnumerable<TFirst>, IEnumerable<TSecond>, Func<TFirst,TSecond,TResult>)

來源:
Zip.cs
來源:
Zip.cs
來源:
Zip.cs

將指定的函式套用至兩個序列的對應項目,產生結果的序列。

C#
public static System.Collections.Generic.IEnumerable<TResult> Zip<TFirst,TSecond,TResult> (this System.Collections.Generic.IEnumerable<TFirst> first, System.Collections.Generic.IEnumerable<TSecond> second, Func<TFirst,TSecond,TResult> resultSelector);

類型參數

TFirst

第一個輸入序列的項目型別。

TSecond

第二個輸入序列的項目型別。

TResult

結果序列的項目型別。

參數

first
IEnumerable<TFirst>

要合併的第一個序列。

second
IEnumerable<TSecond>

要合併的第二個序列。

resultSelector
Func<TFirst,TSecond,TResult>

指定如何從兩個序列合併項目的函式。

傳回

IEnumerable<TResult>

IEnumerable<T>,其中包含兩個輸入序列的合併項目。

例外狀況

firstsecondnull

範例

下列程式代碼範例示範如何使用 Zip 方法來合併兩個序列。

C#
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

備註

這個方法是使用延後執行來實作。 立即傳回值是一個物件,會儲存執行動作所需的所有資訊。 除非直接呼叫其 GetEnumerator 方法或在 C# 或 foreachFor Each Visual Basic 中使用 來列舉對象,否則不會執行這個方法所代表的查詢。

方法會將第一個序列的每個元素與在第二個序列中具有相同索引的項目合併。 如果序列的元素數目不相同,方法會合併序列,直到到達其中一個元素的結尾為止。 例如,如果一個序列有三個元素,而另一個序列有四個元素,則結果序列只會有三個元素。

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Zip<TFirst,TSecond,TThird>(IEnumerable<TFirst>, IEnumerable<TSecond>, IEnumerable<TThird>)

來源:
Zip.cs
來源:
Zip.cs
來源:
Zip.cs

產生具有來自三個指定序列之元素的元組序列。

C#
public static System.Collections.Generic.IEnumerable<(TFirst First, TSecond Second, TThird Third)> Zip<TFirst,TSecond,TThird> (this System.Collections.Generic.IEnumerable<TFirst> first, System.Collections.Generic.IEnumerable<TSecond> second, System.Collections.Generic.IEnumerable<TThird> third);

類型參數

TFirst

第一個輸入序列的項目型別。

TSecond

第二個輸入序列的項目型別。

TThird

第三個輸入序列的項目類型。

參數

first
IEnumerable<TFirst>

要合併的第一個序列。

second
IEnumerable<TSecond>

要合併的第二個序列。

third
IEnumerable<TThird>

要合併的第三個序列。

傳回

IEnumerable<ValueTuple<TFirst,TSecond,TThird>>

一連串的元組,其元素會依該順序取自第一個、第二個和第三個序列。

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.NET 6, 7, 8, 9

Zip<TFirst,TSecond>(IEnumerable<TFirst>, IEnumerable<TSecond>)

來源:
Zip.cs
來源:
Zip.cs
來源:
Zip.cs

從兩個指定序列中的元素產生一系列元組。

C#
public static System.Collections.Generic.IEnumerable<(TFirst First, TSecond Second)> Zip<TFirst,TSecond> (this System.Collections.Generic.IEnumerable<TFirst> first, System.Collections.Generic.IEnumerable<TSecond> second);

類型參數

TFirst

第一個輸入序列的項目型別。

TSecond

第二個輸入序列的項目型別。

參數

first
IEnumerable<TFirst>

要合併的第一個序列。

second
IEnumerable<TSecond>

要合併的第二個序列。

傳回

IEnumerable<ValueTuple<TFirst,TSecond>>

取自第一個和第二個序列元素的一系列元組,以該順序排序。

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.NET Core 3.0, Core 3.1, 5, 6, 7, 8, 9