Skip to content

C# LINQ Zip — Complete Guide

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about C# LINQ Zip. We cover key concepts, practical examples, and best practices.

You have two parallel lists — names and scores — where the first name corresponds to the first score. You loop with an index to pair them. The LINQ Zip method combines two sequences element by element into a single sequence.

Wrong

var names = new[] { "Alice", "Bob", "Charlie" };
var scores = new[] { 85, 92, 78 };
var paired = new List<(string, int)>();

for (int i = 0; i < names.Length && i < scores.Length; i++)
{
    paired.Add((names[i], scores[i]));
}

Output: [("Alice", 85), ("Bob", 92), ("Charlie", 78)] — works, but requires manual index management and bounds checking.

var names = new[] { "Alice", "Bob", "Charlie" };
var scores = new[] { 85, 92, 78 };
var paired = names.Zip(scores).ToList();
// [("Alice", 85), ("Bob", 92), ("Charlie", 78)]

Output: Same tuples. Zip pairs elements from the two sequences at the same position.

With a result selector (C# 9+):

var result = names.Zip(scores, (name, score) => $"{name}: {score}").ToList();
// ["Alice: 85", "Bob: 92", "Charlie: 78"]

Zip stops when the shorter sequence ends:

var a = new[] { 1, 2, 3 };
var b = new[] { 10, 20 };
var zipped = a.Zip(b).ToList(); // [(1, 10), (2, 20)] — 3rd element dropped

Prevention

  • Use Zip to combine parallel sequences instead of indexed loops.
  • Use Zip with a result selector for immediate transformation.
  • Use Zip with three sequences in .NET 6+: a.Zip(b, c).
  • Use Zip with Select for complex combinations.
  • Be aware that Zip truncates to the shorter sequence — ensure equal lengths or handle accordingly.
  • Use Enumerable.Range with Zip to add sequential numbers to a sequence.

Common Mistakes with linq zip

  1. Using foldl instead of foldl' causing stack overflow on large lists
  2. Forgetting deriving (Show, Eq) on custom data types needed for debugging
  3. Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable

These mistakes appear frequently in real-world CSHARP code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

What happens if the sequences are different lengths?

Zip processes elements up to the length of the shorter sequence. Excess elements from the longer sequence are ignored. No exception is thrown. If mismatched length is an error, check lengths before calling Zip.

Can I Zip three sequences?

In .NET 6+, a.Zip(b, c) returns IEnumerable<(TFirst, TSecond, TThird)>. For earlier versions, chain Zip calls: a.Zip(b).Select(t => (t.First, t.Second, c.ElementAt(...))) — but indexing is inefficient. Use a loop for three sequences in older .NET versions.

Is Zip lazy or eager?

Zip is lazy — it reads from both sequences on demand as you iterate. Each MoveNext() advances both enumerators simultaneously. Use .ToList() to materialize the zipped result.

Zip is used in Doda Browser to combine UI labels with their localized translations. For more LINQ, visit DodaTech.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro