typestar

Zip in C#

Walk parallel sequences in step.

var names = new[] { "latency", "errors", "uptime" };
var values = new[] { 42.1, 0.2, 99.95 };
var units = new[] { "ms", "%", "%" };

// Zip walks sequences in step, stopping at the shortest
foreach (var (name, value) in names.Zip(values))
{
    Console.WriteLine($"{name}: {value}");
}

// the three-way overload yields triples
foreach (var (name, value, unit) in names.Zip(values, units))
{
    Console.WriteLine($"{name,-8} {value}{unit}");
}

How it works

  1. names.Zip(values) pairs elements until the shorter side ends.
  2. The pairs deconstruct straight into loop variables.
  3. A third sequence makes triples with the same shape.

Keywords and builtins used here

The run, in numbers

Lines
15
Characters to type
436
Tokens
98
Three-star pace
90 tpm

At the three-star pace of 90 tokens a minute, this run takes about 65 seconds.

Type this snippet

Step 3 of 3 in Shaping, step 3 of 17 in LINQ in depth.

← Previous Next →