Challenge - 5 Problems
LINQ Extension Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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 + " "); } } }
Attempts:
2 left
💡 Hint
Remember that Where filters elements based on the condition.
✗ Incorrect
The Where extension method filters the array to include only even numbers (n % 2 == 0). So only 2 and 4 are printed.
🧠 Conceptual
intermediate1:30remaining
Why does LINQ use extension methods?
Why does LINQ rely on extension methods in C#?
Attempts:
2 left
💡 Hint
Think about how LINQ adds query capabilities to collections.
✗ Incorrect
Extension methods let LINQ add query methods like Where and Select to existing types like IEnumerable without changing their source code.
🔧 Debug
advanced2: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()); } }
Attempts:
2 left
💡 Hint
What happens when you call a LINQ extension method with a null source?
✗ Incorrect
Calling Where on a null IEnumerable causes an ArgumentNullException at runtime because LINQ validates that the source is not null.
📝 Syntax
advanced2: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;
Attempts:
2 left
💡 Hint
Extension methods must be static and have 'this' before the first parameter.
✗ Incorrect
Option C correctly defines a static extension method with 'this' keyword on the first parameter and uses Func delegate.
🚀 Application
expert3: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?
Attempts:
2 left
💡 Hint
Think about what each LINQ method returns and how that allows calling another method.
✗ Incorrect
Each LINQ extension method returns an IEnumerable representing the filtered or transformed sequence, so you can call another LINQ method on it, enabling chaining.