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

Custom LINQ extension methods in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Custom LINQ Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a custom LINQ extension method

What is the output of this C# code using a custom LINQ extension method?

C Sharp (C#)
using System;
using System.Collections.Generic;
using System.Linq;

public static class Extensions
{
    public static IEnumerable<int> DoubleEvenNumbers(this IEnumerable<int> source)
    {
        foreach (var num in source)
        {
            if (num % 2 == 0)
                yield return num * 2;
        }
    }
}

class Program
{
    static void Main()
    {
        var numbers = new List<int> {1, 2, 3, 4, 5};
        var result = numbers.DoubleEvenNumbers();
        foreach (var n in result)
            Console.Write(n + " ");
    }
}
A4 8
B1 3 5
C2 4
D1 2 3 4 5
Attempts:
2 left
💡 Hint

Look at which numbers are even and how they are transformed.

🧠 Conceptual
intermediate
1:30remaining
Purpose of 'this' keyword in extension methods

In C#, what does the this keyword before the first parameter in an extension method signify?

AIt marks the method as static.
BIt indicates the method is an extension method for the type of the parameter.
CIt makes the method private.
DIt means the method can only be called inside the class.
Attempts:
2 left
💡 Hint

Think about how extension methods add functionality to existing types.

🔧 Debug
advanced
2:30remaining
Identify the error in this custom LINQ extension method

What error will this code produce when compiled?

C Sharp (C#)
public static class Extensions
{
    public static IEnumerable<int> FilterOdds(this IEnumerable<int> source)
    {
        return source.Where(x => x % 2 == 1);
    }

    public static IEnumerable<int> FilterOdds(this List<int> source)
    {
        return source.Where(x => x % 2 == 1);
    }
}
ACompile-time error: Duplicate method name with same signature
BCompile-time error: Missing return statement
CRuntime error: StackOverflowException
DNo error, compiles and runs fine
Attempts:
2 left
💡 Hint

Check method signatures and how overload resolution works for extension methods.

📝 Syntax
advanced
2:00remaining
Correct syntax for a custom LINQ extension method with multiple parameters

Which option shows the correct syntax for an extension method that filters strings by minimum length?

Apublic static IEnumerable<string> FilterByLength(IEnumerable<string> source, int minLength) { return source.Where(s => s.Length >= minLength); }
Bpublic IEnumerable<string> FilterByLength(IEnumerable<string> source, int minLength) { return source.Where(s => s.Length >= minLength); }
Cpublic static IEnumerable<string> FilterByLength(this IEnumerable<string> source, int minLength) { return source.Where(s => s.Length >= minLength); }
Dpublic static IEnumerable<string> FilterByLength(this IEnumerable<string> source) { return source.Where(s => s.Length >= minLength); }
Attempts:
2 left
💡 Hint

Remember extension methods must be static and the first parameter uses this.

🚀 Application
expert
3:00remaining
Result count after chaining custom LINQ extension methods

Given these two extension methods, what is the count of items in result?

C Sharp (C#)
using System;
using System.Collections.Generic;
using System.Linq;

public static class Extensions
{
    public static IEnumerable<int> MultiplyBy(this IEnumerable<int> source, int factor)
    {
        foreach (var n in source)
            yield return n * factor;
    }

    public static IEnumerable<int> FilterGreaterThan(this IEnumerable<int> source, int threshold)
    {
        foreach (var n in source)
            if (n > threshold)
                yield return n;
    }
}

class Program
{
    static void Main()
    {
        var numbers = new List<int> {1, 2, 3, 4, 5};
        var result = numbers.MultiplyBy(3).FilterGreaterThan(10);
        Console.WriteLine(result.Count());
    }
}
A2
B0
C3
D1
Attempts:
2 left
💡 Hint

Multiply each number by 3, then count how many are greater than 10.