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
names.Zip(values)pairs elements until the shorter side ends.- The pairs deconstruct straight into loop variables.
- A third sequence makes triples with the same shape.
Keywords and builtins used here
foreachinnewvaluevar
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.
Step 3 of 3 in Shaping, step 3 of 17 in LINQ in depth.