Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method name that does not clearly describe its purpose.
✗ Incorrect
The method name 'FirstChar' clearly describes the extension method's purpose.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to call the method as a static method instead of an instance method.
✗ Incorrect
The extension method 'FirstChar' is called like a normal method on the string instance.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting the 'this' keyword, which causes the method not to be an extension method.
✗ Incorrect
The 'this' keyword before the first parameter marks it as an extension method.
4fill in blank
hardComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect lambda syntax or method name.
✗ Incorrect
The method name 'IsDigitsOnly' describes the check, and '=>' is the lambda arrow used in the All method.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong parameter names or method names.
Not using Enumerable.Repeat correctly.
✗ Incorrect
The method name is 'Repeat', the parameter is 'count', and the Enumerable method is 'Repeat' to repeat the string.