Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare an extension method for the string type 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 indicate it returns the first character.
Forgetting to make the method static or the class static.
✗ Incorrect
The method name 'First' is a common and clear name for returning the first character of a string.
2fill in blank
mediumComplete the code to call the extension method 'IsEven' on an integer variable.
C Sharp (C#)
int number = 4; bool result = number.[1]();
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong method name that does not exist.
Calling a method that checks for odd instead of even.
✗ Incorrect
The extension method is named 'IsEven', so calling number.IsEven() is correct.
3fill in blank
hardFix the error in the extension method declaration by completing the code.
C Sharp (C#)
public static class IntExtensions { public static bool [1](this int value) { return value % 2 == 0; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Naming the method 'IsOdd' which contradicts the logic.
Using a vague or unclear method name.
✗ Incorrect
The method name 'IsEven' matches the logic that returns true if the number is even.
4fill in blank
hardFill both blanks to create an extension method that returns the last character of a string.
C Sharp (C#)
public static class StringHelpers { public static char [1](this string str) { return str[2]; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect indexing syntax.
Choosing a method name that does not match the functionality.
✗ Incorrect
The method name 'LastChar' clearly describes the purpose, and accessing str[str.Length - 1] returns the last character.
5fill in blank
hardFill all three blanks to create an extension method that repeats a string n times.
C Sharp (C#)
public static class StringExtensions { 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 incorrect parameter names.
Using wrong method names or missing the Enumerable.Repeat call.
✗ Incorrect
The method name 'Repeat' describes the action, 'count' is a common parameter name for number of repetitions, and Enumerable.Repeat is the correct method to repeat a string.