Linq Zip Method in C#

Linq Zip in C# with Example

In this article, I am going to discuss the LINQ Zip in C# with examples. Please read our previous article before proceeding to this article where we discussed the LINQ Prepend Method with an example. At the end of this article, you will understand what is Linq Zip method and the need and use of C# Linq Zip Method with examples.

Linq Zip Method:

The Linq Zip Method in C# is used to apply a specified function to the corresponding elements of two sequences and producing a sequence of the results. The signature of this method is given below.

Linq Zip Method in C# with Examples

Type Parameters:
  1. TFirst: The type of the elements of the first input sequence.
  2. TSecond: The type of the elements of the second input sequence.
  3. TResult: The type of the elements of the result sequence.
  1. IEnumerable<TFirst> first: The first sequence to merge.
  2. IEnumerable<TSecond> second: The second sequence to merge.
  3. Func<TFirst,TSecond,TResult> resultSelector: A function that specifies how to merge the elements from the two sequences.

Returns: This method is going to return an IEnumerable<T> that contains merged elements of two input sequences.

Exceptions: This method is going to throw ArgumentNullException when either the first or the second input sequence is null.

Note: The Zip method merges each element of the first sequence with an element in the second sequence that has the same index position. If both the sequences do not have the same number of elements, then the Zip method merges sequences until it reaches the end of the sequence which contains fewer elements. For example, if one sequence has five elements and the other sequence has four elements, then the result sequence will have only four elements.


The following example shows how to merge two sequences using the Linq Zip method.

using System;
using System.Linq;

namespace ThreadingDemo
    class Program
        static void Main(string[] args)
            int[] numbersSequence = { 10, 20, 30, 40, 50 };
            string[] wordsSequence = { "Ten", "Twenty", "Thirty", "Fourty" };

            var resultSequence = numbersSequence.Zip(wordsSequence, (first, second) => first + " - " + second);

            foreach (var item in resultSequence)


ZIP Method Output in Linq

The first sequence contains 5 elements whereas the second sequence contains 4 elements. So, for the fifth element of the first sequence, there is no corresponding fifth element in the second sequence. As a result, the Zip method merges the four elements and that’s what we have seen in the output.


The Zip method is implemented by using the deferred execution. So, the immediate return value of this method is going to be an object which stores all the required information which is required to perform the action. The query represented by this method is not executed until the object is enumerated either by calling its GetEnumerator method directly or by using for each loop.

That’s it for today. I hope you understood the Linq ZIP in C#. In the next article, I am going to discuss the Deferred Execution VS Immediate Execution in LINQ with some examples.

Leave a Reply

Your email address will not be published. Required fields are marked *