Recall & Review
beginner
What does the
Zip method do in C#?The
Zip method combines two sequences element-by-element into a single sequence of pairs or results from a function.Click to reveal answer
beginner
How does
Zip handle sequences of different lengths?The
Zip method stops combining elements when the shortest sequence ends, ignoring extra elements in the longer sequence.Click to reveal answer
beginner
Show a simple example of
Zip combining two integer arrays.Example:<br>
var a = new[] {1, 2, 3};
var b = new[] {4, 5, 6};
var zipped = a.Zip(b, (x, y) => x + y);
// Result: [5, 7, 9]Click to reveal answer
intermediate
Can
Zip be used with sequences of different types?Yes,
Zip can combine sequences of different types by specifying a result selector function that defines how to combine elements.Click to reveal answer
beginner
What namespace must be included to use
Zip in C#?You must include
using System.Linq; to use the Zip method.Click to reveal answer
What happens if the two sequences passed to
Zip have different lengths?✗ Incorrect
The
Zip method stops combining elements when the shortest sequence ends.Which namespace is required to use
Zip in C#?✗ Incorrect
Zip is part of LINQ, so System.Linq is required.What does this code return?<br>
var a = new[] {1, 2};
var b = new[] {3, 4, 5};
var result = a.Zip(b, (x, y) => x * y).ToList();✗ Incorrect
The result length matches the shorter array (length 2), so only first two pairs are multiplied.
Can
Zip combine sequences of different types?✗ Incorrect
Zip allows combining different types by specifying a result selector function.What is the output of this code?<br>
var names = new[] {"Anna", "Bob"};
var ages = new[] {25, 30};
var zipped = names.Zip(ages, (name, age) => $"{name} is {age} years old");
foreach(var s in zipped) Console.WriteLine(s);✗ Incorrect
The
Zip combines names and ages into formatted strings.Explain how the
Zip method works in C# and give a simple example.Think about pairing elements from two lists.
You got /4 concepts.
What happens if the sequences passed to
Zip have different lengths? How can you handle this in your code?Consider what happens to leftover elements.
You got /3 concepts.