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

Why extension methods are needed 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 string that returns the first character.

C Sharp (C#)
public static class StringExtensions {
    public static char [1](this string str) {
        return str[0];
    }
}
Drag options to blanks, or click blank then click option'
AFirstChar
BGetFirst
CStartChar
DCharAt
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method name that does not clearly describe its purpose.
2fill in blank
medium

Complete the code to call the extension method on a string variable.

C Sharp (C#)
string name = "Alice";
char first = name.[1]();
Drag options to blanks, or click blank then click option'
AGetFirst
BCharAt
CStartChar
DFirstChar
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to call the method as a static method instead of an instance method.
3fill in blank
hard

Fix the error in the extension method definition by completing the missing keyword.

C Sharp (C#)
public static class StringExtensions {
    public static char FirstChar([1] string str) {
        return str[0];
    }
}
Drag options to blanks, or click blank then click option'
Apublic
Bstatic
Cthis
Dreadonly
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting the 'this' keyword, which causes the method not to be an extension method.
4fill in blank
hard

Complete the code to create an extension method that checks if a string contains only digits.

C Sharp (C#)
public static bool [1](this string str) {
    return str.All(c => char.IsDigit(c));
}
Drag options to blanks, or click blank then click option'
AIsDigitsOnly
B=>
C=>=
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect lambda syntax or method name.
5fill in blank
hard

Fill all three blanks to create an extension method that repeats a string n times.

C Sharp (C#)
public static string [1](this string str, int [2]) {
    return string.Concat(Enumerable.[3](str, [2]));
}
Drag options to blanks, or click blank then click option'
ARepeat
Bcount
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong parameter names or method names.
Not using Enumerable.Repeat correctly.