Skip to content

Instantly share code, notes, and snippets.

@kersny
Created July 11, 2010 02:56
Show Gist options
  • Save kersny/471233 to your computer and use it in GitHub Desktop.
Save kersny/471233 to your computer and use it in GitHub Desktop.
//An Implementation of Enumerable.Zip for .NET 3.5 or Mono
//Taken from Stackoverflow Question 1616554
//http://stackoverflow.com/questions/1616554/create-an-enumeratordatatype-datatype-from-2-enumerables
//Originally published by Doug McClean
//Posted here in case I need it again
public static class EnumerableExtensions
{
public static IEnumerable<TResult> Zip<TFirst, TSecond, TResult>(
IEnumerable<TFirst> first,
IEnumerable<TSecond> second,
Func<TFirst, TSecond, TResult> resultSelector)
{
using(var firstEnum = first.GetEnumerator())
using(var secondEnum = second.GetEnumerator())
{
while(firstEnum.MoveNext() && secondEnum.MoveNext())
{
yield return resultSelector(firstEnum.Current, secondEnum.Current);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment