Ad space (banner)
#๏ธโƒฃC# Lessons
Lesson 44 / 68

Pairing Up Two Arrays (the zip operation)

In this lesson you will learn how to pair up two arrays with C#'s Zip method, so related data can be handled together. This is for anyone searching for "C# LINQ Zip how to use".

Combining two corresponding arrays - a list of names and a list of scores, say - into pairs one by one is called a zip operation. LINQ provides .Zip().

The sample code uses names.Zip(scores, (n, s) => $"({n}, {s})") to pair up the elements at matching positions in names and scores, formatting each pair readably with string interpolation.

A common stumbling block for beginners is what happens when the two arrays are different lengths. .Zip() stops at the shorter one, so any leftovers are simply not included.

In real-world development, it is a handy LINQ feature whenever you want to loop over several lists at once. For three or more lists, chaining Zip calls works.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run C# directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)