Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

Enumerable.Zip: practical reference

Enumerable.Zip: practical reference: The deferred operation combines elements at equal positions and stops when the shortest source is exhausted.

Version: .NET LINQ; verified on .NET 10.0.14

Syntax

source.Zip(/* overload-specific arguments */)

The deferred operation combines elements at equal positions and stops when the shortest source is exhausted.

Use it to pair parallel sequences when truncation to the shorter one is correct.

Extra elements in a longer source are omitted. Failing to validate equal lengths can hide data loss.

If any source is empty, the result is empty. A null source or resultSelector causes ArgumentNullException.

The complete program actually uses Enumerable.Zip; its exact expected stdout is 1a,2b.

Examples

using System;
using System.Collections.Generic;
using System.Linq;

Console.WriteLine(string.Join(",", new[] { 1, 2 }.Zip(new[] { "a", "b", "c" }, (number, text) => $"{number}{text}")));

Result

1a,2b

The complete program actually uses `Enumerable.Zip`; its exact expected stdout is `1a,2b`.

Sources