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

Extension methods for built-in types 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 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'
AFirstChar
BFirst
CGetFirst
DCharAt
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.
2fill in blank
medium

Complete 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'
AIsEven
BCheckEven
CEvenCheck
DIsOdd
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.
3fill in blank
hard

Fix 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'
ACheckEven
BIsOdd
CIsEven
DEven
Attempts:
3 left
💡 Hint
Common Mistakes
Naming the method 'IsOdd' which contradicts the logic.
Using a vague or unclear method name.
4fill in blank
hard

Fill 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'
ALastChar
BLength - 1
C[str.Length - 1]
DLast
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect indexing syntax.
Choosing a method name that does not match the functionality.
5fill in blank
hard

Fill 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'
ARepeat
Bcount
Dtimes
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect parameter names.
Using wrong method names or missing the Enumerable.Repeat call.