Recall & Review
beginner
What is a LINQ extension method in C#?
A LINQ extension method is a special method that adds new query capabilities to existing types, allowing you to write queries in a fluent style using dot notation.
Click to reveal answer
beginner
How do you define a custom LINQ extension method?
You define a custom LINQ extension method as a static method inside a static class, where the first parameter uses the 'this' keyword to specify the type it extends.
Click to reveal answer
beginner
Why use custom LINQ extension methods?
Custom LINQ extension methods let you create reusable, readable, and chainable query operations tailored to your needs, making your code cleaner and easier to understand.Click to reveal answer
intermediate
What is the role of 'IEnumerable<T>' in custom LINQ extension methods?
Most custom LINQ extension methods extend 'IEnumerable<T>' because it represents a sequence of elements that can be queried, allowing your method to work with any collection that supports enumeration.
Click to reveal answer
beginner
Show a simple example of a custom LINQ extension method that returns only even numbers from a sequence.
public static class MyExtensions
{
public static IEnumerable<int> EvenNumbers(this IEnumerable<int> source)
{
foreach (var num in source)
{
if (num % 2 == 0)
yield return num;
}
}
}Click to reveal answer
What keyword is used to define an extension method's first parameter?
✗ Incorrect
The 'this' keyword before the first parameter tells the compiler this is an extension method for that type.
Where must extension methods be declared?
✗ Incorrect
Extension methods must be inside a static class to be recognized by the compiler.
Which interface do most LINQ extension methods extend?
✗ Incorrect
LINQ methods typically extend IEnumerable to work with any enumerable collection.
What does the 'yield return' statement do in an extension method?
✗ Incorrect
'yield return' allows the method to return elements one by one as they are requested.
Why create custom LINQ extension methods?
✗ Incorrect
Custom LINQ extension methods help add reusable and readable query logic tailored to your needs.
Explain how to create a custom LINQ extension method and why it is useful.
Think about how you add new features to existing types without changing them.
You got /5 concepts.
Describe the role of 'yield return' in a custom LINQ extension method.
Imagine handing out items one by one instead of all at once.
You got /4 concepts.