Codectionary / Developer documentation / C#

Combine Sequences by Position with Zip

Pair corresponding positions and recognise that the shortest sequence limits the result.

Read the syntax

Pair corresponding positions and recognise that the shortest sequence limits the result.

The names sequence ends after two elements.

What it is and why to use it

Two already aligned lists, such as names and corresponding scores, can be combined by position. Zip pairs the first elements, then the second and so on. It does not match keys like Join and does not generate every possible combination. Use it only when positional alignment is part of the data contract.

How it works

The two-sequence overload can return tuples or apply a result selector. The documented environment also supports a three-sequence tuple overload. Enumeration is deferred and stops when any input ends. Extra elements in longer inputs are not emitted or treated as an error automatically.

Common mistakes and limitations

Do not silently zip mismatched data if losing unmatched elements would be incorrect. Validate lengths when the contract requires equal-sized inputs. Avoid enumerating single-pass sources just to count them and then assuming they can be replayed unchanged.

Run the examples locally

These complete Program.cs examples use a .NET 10 console project with the C# language version set to 12.0, nullable checking enabled and implicit usings disabled. They use standard .NET libraries and need no extra NuGet packages. This is the verified environment, not a claim that every API requires .NET 10 or works unchanged in .NET Framework, Unity or other runtimes.

Install a supported .NET 10 SDK, create the console project, replace its project file with the settings below, then replace Program.cs with one complete example at a time. Build and run from the project folder. using directives bring namespaces into scope; top-level statements form the program’s entry point, and any helper types follow those statements. Console.WriteLine writes terminal output. Do not combine examples that declare the same type names.

C# in Codectionary’s Playground is editing-only: it does not compile or execute these programs. Run them with the local .NET SDK to reproduce the expected output. Async examples use controlled in-process operations rather than live network services or timing-sensitive delays.

Create and enter a project

dotnet new console -n CSharpExample --framework net10.0
cd CSharpExample

CSharpExample.csproj settings used for these examples

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net10.0</TargetFramework>
    <LangVersion>12.0</LangVersion>
    <Nullable>enable</Nullable>
    <ImplicitUsings>disable</ImplicitUsings>
  </PropertyGroup>
</Project>

Build and run after saving one example in Program.cs

dotnet build
dotnet run --no-build

Continue learning

Change one input and predict the result before running it. Follow the related guides to connect this topic with the surrounding language features.

Syntax

var pairs = names.Zip(scores, (name, score) => $"{name}: {score}");

Examples

Pair names with scores

Observe that the extra score has no matching name.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

string[] names = { "John", "James" };
int[] scores = { 80, 90, 100 };
foreach (var line in names.Zip(scores, (name, score) => $"{name}: {score}"))
    Console.WriteLine(line);
  1. Zip pairs the two first positions and then the two second positions.
  2. The names sequence ends after two elements.
  3. Score 100 is not emitted. Use an explicit mismatch check if every score must belong to a name.
  4. Expected output: John: 80 James: 90
  5. Use Zip only for data that is meaningfully aligned by position.

Align three numeric series

Use the three-sequence overload and name each tuple component.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

int[] first = { 1, 2 };
int[] second = { 10, 20 };
int[] third = { 100 };
foreach (var row in first.Zip(second, third))
    Console.WriteLine(row.First + row.Second + row.Third);
  1. The returned tuple exposes First, Second and Third components.
  2. Only the first position exists in every input sequence.
  3. The sum is 111 and there is no second row. This overload is available in the documented .NET environment.
  4. Expected output: 111
  5. Use Zip only for data that is meaningfully aligned by position.

Best practices

  • Use Zip only for data that is meaningfully aligned by position.
  • Validate unequal lengths when truncation is unacceptable.

Runtime & versions

.NET runtime. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

C# language reference

Specifications & further reading