英語で読む

次の方法で共有


Enumerable.Zip メソッド

定義

オーバーロード

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

2 つのシーケンスの対応する要素に対して、1 つの指定した関数を適用し、結果として 1 つのシーケンスを生成します。

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

指定された 3 つのシーケンスの要素を含むタプルのシーケンスを生成します。

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

指定された 2 つのシーケンスの要素を持つタプルのシーケンスを生成します。

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

ソース:
Zip.cs
ソース:
Zip.cs
ソース:
Zip.cs

2 つのシーケンスの対応する要素に対して、1 つの指定した関数を適用し、結果として 1 つのシーケンスを生成します。

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

1 番目の入力シーケンスの要素の型。

TSecond

2 番目の入力シーケンスの要素の型。

TResult

結果のシーケンスの要素の型。

パラメーター

first
IEnumerable<TFirst>

マージする 1 番目のシーケンス。

second
IEnumerable<TSecond>

マージする 2 番目のシーケンス。

resultSelector
Func<TFirst,TSecond,TResult>

2 つのシーケンスの要素をマージする方法を指定する関数。

戻り値

IEnumerable<TResult>

2 つの入力シーケンスのマージされた要素が格納されている IEnumerable<T>

例外

first または secondnull です。

次のコード例では、 メソッドを使用 Zip して 2 つのシーケンスをマージする方法を示します。

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# For Each または Visual Basic で を使用foreachして列挙されるまで実行されません。

メソッドは、最初のシーケンスの各要素を、2 番目のシーケンスで同じインデックスを持つ要素とマージします。 シーケンスに同じ数の要素がない場合、メソッドはいずれかの要素の末尾に達するまでシーケンスをマージします。 たとえば、1 つのシーケンスに 3 つの要素があり、もう 1 つのシーケンスに 4 つの要素がある場合、結果シーケンスには 3 つの要素のみが含まれます。

こちらもご覧ください

適用対象

.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

指定された 3 つのシーケンスの要素を含むタプルのシーケンスを生成します。

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

1 番目の入力シーケンスの要素の型。

TSecond

2 番目の入力シーケンスの要素の型。

TThird

3 番目の入力シーケンスの要素の型。

パラメーター

first
IEnumerable<TFirst>

マージする 1 番目のシーケンス。

second
IEnumerable<TSecond>

マージする 2 番目のシーケンス。

third
IEnumerable<TThird>

マージする 3 番目のシーケンス。

戻り値

IEnumerable<ValueTuple<TFirst,TSecond,TThird>>

1 番目、2 番目、および 3 番目のシーケンスから取得された要素を持つタプルのシーケンス 。その順序。

こちらもご覧ください

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET 6, 7, 8, 9

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

ソース:
Zip.cs
ソース:
Zip.cs
ソース:
Zip.cs

指定された 2 つのシーケンスの要素を持つタプルのシーケンスを生成します。

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

1 番目の入力シーケンスの要素の型。

TSecond

2 番目の入力シーケンスの要素の型。

パラメーター

first
IEnumerable<TFirst>

マージする 1 番目のシーケンス。

second
IEnumerable<TSecond>

マージする 2 番目のシーケンス。

戻り値

IEnumerable<ValueTuple<TFirst,TSecond>>

最初のシーケンスと 2 番目のシーケンスから取得された要素が、その順序で備えられているタプルのシーケンス。

こちらもご覧ください

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 3.0, Core 3.1, 5, 6, 7, 8, 9