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

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

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 for IEnumerable<int>.

C Sharp (C#)
public static class Extensions {
    public static int Sum(this IEnumerable<int> [1]) {
        int total = 0;
        foreach (var num in source) {
            total += num;
        }
        return total;
    }
}
Drag options to blanks, or click blank then click option'
Asource
Bitem
Clist
Dcollection
Attempts:
3 left
💡 Hint
Common Mistakes
Using a parameter name that is not descriptive or not valid as an identifier.
Forgetting the this keyword before the parameter.
2fill in blank
medium

Complete the code to call the extension method on a list.

C Sharp (C#)
var numbers = new List<int> {1, 2, 3};
int total = numbers.[1]();
Drag options to blanks, or click blank then click option'
AAdd
BSort
CCount
DSum
Attempts:
3 left
💡 Hint
Common Mistakes
Using methods that do not return a sum like Add or Sort.
Forgetting that extension methods appear as if they are instance methods.
3fill in blank
hard

Fix the error in the extension method declaration.

C Sharp (C#)
public static int CountItems([1]) {
    int count = 0;
    foreach (var item in source) {
        count++;
    }
    return count;
}
Drag options to blanks, or click blank then click option'
Athis IEnumerable<int> source
Bthis source
Csource
DIEnumerable<int> this source
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting the this keyword.
Placing this after the type or parameter name incorrectly.
4fill in blank
hard

Fill both blanks to create an extension method that filters even numbers.

C Sharp (C#)
public static IEnumerable<int> [1](this IEnumerable<int> source) {
    foreach (var num in source) {
        if (num [2] 2 == 0) {
            yield return num;
        }
    }
}
Drag options to blanks, or click blank then click option'
AWhereEven
BSum
C%
DAdd
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect method names like Sum or Add.
Using wrong operators like + instead of modulus.
5fill in blank
hard

Fill all three blanks to create an extension method that converts strings to uppercase and filters by length.

C Sharp (C#)
public static IEnumerable<string> [1](this IEnumerable<string> source, int minLength) {
    foreach (var str in source) {
        var upper = str.[2]();
        if (upper.Length [3] minLength) {
            yield return upper;
        }
    }
}
Drag options to blanks, or click blank then click option'
AFilterAndUpper
BToUpper
C>=
DToLower
Attempts:
3 left
💡 Hint
Common Mistakes
Using ToLower instead of ToUpper.
Using wrong comparison operators like <.