Search code examples
c#linqenumerable

What is the use of Enumerable.Zip extension method in Linq?


What is the use of Enumerable.Zip extension method in Linq?


Solution

  • The Zip operator merges the corresponding elements of two sequences using a specified selector function.

    var letters= new string[] { "A", "B", "C", "D", "E" };
    var numbers= new int[] { 1, 2, 3 };
    var q = letters.Zip(numbers, (l, n) => l + n.ToString());
    foreach (var s in q)
        Console.WriteLine(s);
    

    Ouput

    A1
    B2
    C3