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

Why LINQ depends on extension methods in C Sharp (C#) - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
LINQ Extension Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this LINQ extension method call?
Consider the following C# code using LINQ extension methods. What will be printed to the console?
C Sharp (C#)
using System;
using System.Linq;

class Program {
    static void Main() {
        int[] numbers = {1, 2, 3, 4, 5};
        var evens = numbers.Where(n => n % 2 == 0);
        foreach(var num in evens) {
            Console.Write(num + " ");
        }
    }
}
ACompilation error
B1 2 3 4 5
C1 3 5
D2 4
Attempts:
2 left
💡 Hint
Remember that Where filters elements based on the condition.
🧠 Conceptual
intermediate
1:30remaining
Why does LINQ use extension methods?
Why does LINQ rely on extension methods in C#?
ABecause extension methods allow adding new methods to existing types without modifying them.
BBecause extension methods improve runtime performance significantly.
CBecause extension methods are the only way to create static methods in C#.
DBecause extension methods automatically generate SQL queries.
Attempts:
2 left
💡 Hint
Think about how LINQ adds query capabilities to collections.
🔧 Debug
advanced
2:00remaining
What error does this LINQ extension method code produce?
Examine the code below. What error will occur when compiling or running it?
C Sharp (C#)
using System;
using System.Linq;

class Program {
    static void Main() {
        int[] numbers = null;
        var result = numbers.Where(n => n > 0);
        Console.WriteLine(result.Count());
    }
}
AArgumentNullException at runtime
BCompilation error: 'Where' method not found
COutput: 0
DInvalidOperationException at runtime
Attempts:
2 left
💡 Hint
What happens when you call a LINQ extension method with a null source?
📝 Syntax
advanced
2:30remaining
Which option correctly defines a LINQ extension method?
Which of the following is a correct way to define a LINQ-style extension method for IEnumerable?
C Sharp (C#)
using System.Collections.Generic;
Apublic static IEnumerable<T> Filter<T>(IEnumerable<T> source, Func<T, bool> predicate) { foreach(var item in source) if(predicate(item)) yield return item; }
Bpublic IEnumerable<T> Filter<T>(IEnumerable<T> source, Func<T, bool> predicate) { foreach(var item in source) if(predicate(item)) yield return item; }
Cpublic static IEnumerable<T> Filter<T>(this IEnumerable<T> source, Func<T, bool> predicate) { foreach(var item in source) if(predicate(item)) yield return item; }
Dpublic static IEnumerable<T> Filter<T>(this IEnumerable<T> source, bool predicate(T item)) { foreach(var item in source) if(predicate(item)) yield return item; }
Attempts:
2 left
💡 Hint
Extension methods must be static and have 'this' before the first parameter.
🚀 Application
expert
3:00remaining
How does LINQ's use of extension methods enable query chaining?
LINQ methods like Where and Select can be chained together fluently. How does the use of extension methods enable this chaining?
ABecause extension methods modify the original collection in place to add query results.
BBecause each extension method returns an IEnumerable<T>, allowing the next method to be called on the result.
CBecause extension methods compile queries into SQL automatically for chaining.
DBecause extension methods use reflection to dynamically add methods at runtime.
Attempts:
2 left
💡 Hint
Think about what each LINQ method returns and how that allows calling another method.