ソースコード
using System.Collections.Generic;
using System.Linq;
public static class IEnumerableExt
{
public static IEnumerable<(T item1, T item2)> Combine<T>
(
this IEnumerable<T> first,
IEnumerable<T> second
)
{
return first.SelectMany( i1 => second.Select( i2 => ( i1, i2 ) ) );
}
}
通常
var list1 = new[] { 1, 2, 3 };
var list2 = new[] { 1, 2, 3 };
foreach ( var i1 in list1 )
{
foreach ( var i2 in list2 )
{
Console.WriteLine( i1 + ", " + i2 );
}
}
拡張メソッド
var list1 = new[] { 1, 2, 3 };
var list2 = new[] { 1, 2, 3 };
foreach ( var ( x, y ) in list1.Combine( list2 ) )
{
Console.WriteLine( x + ", " + y );
}