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

Extension method syntax 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 declare an extension method for string that returns the first character.

C Sharp (C#)
public static char [1](this string str) {
    return str[0];
}
Drag options to blanks, or click blank then click option'
AToUpper
BFirstChar
CLength
DSubstring
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method name that already exists in string like ToUpper.
Not using a descriptive method name.
2fill in blank
medium

Complete the code to declare the extension method inside a static class named StringExtensions.

C Sharp (C#)
public static class [1] {
    public static bool IsNullOrEmpty(this string str) {
        return str == null || str == "";
    }
}
Drag options to blanks, or click blank then click option'
AStringExtensions
BMyExtensions
CHelper
DExtensions
Attempts:
3 left
💡 Hint
Common Mistakes
Declaring the class as non-static.
Using a different class name than 'StringExtensions'.
3fill in blank
hard

Fix the error in the extension method declaration by completing the method signature correctly.

C Sharp (C#)
public static int WordCount([1] string str) {
    return str.Split(' ').Length;
}
Drag options to blanks, or click blank then click option'
Apublic
Bstatic
Cthis
Dref
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'static' or 'public' before the parameter instead of 'this'.
Omitting the 'this' keyword.
4fill in blank
hard

Complete the code to create an extension method that returns true 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 like '=='.
Choosing a method name that does not describe the functionality.
5fill in blank
hard

Fill all three blanks to create an extension method that returns a dictionary with words as keys and their lengths as values, filtering words longer than 3 characters.

C Sharp (C#)
public static Dictionary<string, int> [1](this string sentence) {
    return sentence.Split(' ').Where([2] => [2].Length > 3).ToDictionary([2] => [2], [3] => [3].Length);
}
Drag options to blanks, or click blank then click option'
AWordLengths
Bword
Cw
Dwords
Attempts:
3 left
💡 Hint
Common Mistakes
Using different lambda parameters inconsistently.
Choosing a method name unrelated to word lengths.