Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method name that already exists in string like ToUpper.
Not using a descriptive method name.
✗ Incorrect
The method name 'FirstChar' correctly names the extension method that returns the first character of the string.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Declaring the class as non-static.
Using a different class name than 'StringExtensions'.
✗ Incorrect
Extension methods must be inside a static class. 'StringExtensions' is the correct class name as per the instruction.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'static' or 'public' before the parameter instead of 'this'.
Omitting the 'this' keyword.
✗ Incorrect
The 'this' keyword before the first parameter marks it as an extension method parameter.
4fill in blank
hardComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect lambda syntax like '=='.
Choosing a method name that does not describe the functionality.
✗ Incorrect
The method name 'IsDigitsOnly' describes the check. The lambda arrow '=>' is used correctly in the All method.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different lambda parameters inconsistently.
Choosing a method name unrelated to word lengths.
✗ Incorrect
The method name 'WordLengths' fits the purpose. The lambda parameter 'w' is used consistently for keys and values.