Creates a list by combining two other lists into one.
using System.Linq; using System.Diagnostics; namespace System.Collections.Generic { public static class EnumeratorExtensions { /// <summary> /// Creates a list by combining two other lists into one. /// </summary> /// <typeparam name="T1"></typeparam> /// <typeparam name="T2"></typeparam> /// <typeparam name="TResult"></typeparam> /// <param name="source1">One of the lists to zip.</param> /// <param name="source2">One of the lists to zip.</param> /// <param name="zipFunction">The delegate used to combine items.</param> /// <returns>A new list with the combined items.</returns> public static IEnumerable<TResult> Zip<T1, T2, TResult>(this IEnumerable<T1> source1, IEnumerable<T2> source2, Func<T1, T2, TResult> combine) { if (source1 == null) throw new ArgumentNullException("source1"); if (source2 == null) throw new ArgumentNullException("source2"); if (combine == null) throw new ArgumentNullException("combine"); return ZipIterator<T1, T2, TResult>(source1, source2, combine); } private static IEnumerable<TResult> ZipIterator<T1, T2, TResult>(IEnumerable<T1> source1, IEnumerable<T2> source2, Func<T1, T2, TResult> combine) { Debug.Assert(source1 != null, "source1 is null."); Debug.Assert(source2 != null, "source2 is null."); Debug.Assert(combine != null, "combine is null."); using (IEnumerator<T1> data1 = source1.GetEnumerator()) using (IEnumerator<T2> data2 = source2.GetEnumerator()) while (data1.MoveNext() && data2.MoveNext()) { yield return combine(data1.Current, data2.Current); } } } }