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

Custom LINQ extension methods in C Sharp (C#) - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define an extension method that returns the first element of a sequence.

C Sharp (C#)
public static T [1]<T>(this IEnumerable<T> source) {
    foreach (var item in source) {
        return item;
    }
    throw new InvalidOperationException("Sequence contains no elements");
}
Drag options to blanks, or click blank then click option'
AFirst
BSelect
CWhere
DOrderBy
Attempts:
3 left
💡 Hint
Common Mistakes
Using a LINQ method name that does not return a single element.
Forgetting to make the method an extension method with 'this' keyword.
2fill in blank
medium

Complete the code to define an extension method that filters elements based on a predicate.

C Sharp (C#)
public static IEnumerable<T> [1]<T>(this IEnumerable<T> source, Func<T, bool> predicate) {
    foreach (var item in source) {
        if (predicate(item)) {
            yield return item;
        }
    }
}
Drag options to blanks, or click blank then click option'
ASelect
BOrderBy
CWhere
DGroupBy
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Select' which transforms elements instead of filtering.
Not using 'yield return' to return filtered elements lazily.
3fill in blank
hard

Fix the error in the extension method that projects each element to a new form.

C Sharp (C#)
public static IEnumerable<TResult> Select<T, TResult>(this IEnumerable<T> source, Func<T, TResult> selector) {
    foreach (var item in source) {
        yield return [1];
    }
}
Drag options to blanks, or click blank then click option'
Aitem
Bselector(item)
Csource
Dselector
Attempts:
3 left
💡 Hint
Common Mistakes
Returning the item itself instead of applying the selector.
Returning the selector delegate instead of calling it.
4fill in blank
hard

Fill both blanks to create an extension method that counts elements matching a condition.

C Sharp (C#)
public static int Count<T>(this IEnumerable<T> source, Func<T, bool> predicate) {
    int count = 0;
    foreach (var item in source) {
        if (predicate(item) [1] true) {
            count [2] 1;
        }
    }
    return count;
}
Drag options to blanks, or click blank then click option'
A=>
B==
C+=
D-=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=>' which is not a comparison operator.
Using '-=' which decreases the count incorrectly.
5fill in blank
hard

Fill both blanks to create an extension method that returns a dictionary with keys and values selected from the source.

C Sharp (C#)
public static Dictionary<TKey, TValue> ToDictionary<TSource, TKey, TValue>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TValue> valueSelector) {
    var dict = new Dictionary<TKey, TValue>();
    foreach (var item in source) {
        dict.[1](keySelector(item), [2]);
    }
    return dict;
}
Drag options to blanks, or click blank then click option'
AAdd
BvalueSelector(item)
Citem
DRemove
Attempts:
3 left
💡 Hint
Common Mistakes
Using Remove instead of Add.
Passing the item directly instead of the selected value.