0
0
C Sharp (C#)programming~5 mins

Custom LINQ extension methods in C Sharp (C#) - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
Athis
Bstatic
Cpublic
Doverride
Where must extension methods be declared?
AInside any class
BInside a sealed class
CInside an interface
DInside a static class
Which interface do most LINQ extension methods extend?
AIList<T>
BIEnumerable<T>
CIDisposable
DIComparable
What does the 'yield return' statement do in an extension method?
AEnds the method immediately
BReturns all elements at once
CReturns elements one at a time lazily
DThrows an exception
Why create custom LINQ extension methods?
ATo add reusable query logic
BTo replace built-in LINQ methods
CTo make code slower
DTo avoid using IEnumerable
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.