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

Extension methods for built-in types in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Extension Method Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of an extension method on string
What is the output of this C# code that uses an extension method on a string?
C Sharp (C#)
public static class StringExtensions {
    public static string Repeat(this string s, int times) {
        return string.Concat(System.Linq.Enumerable.Repeat(s, times));
    }
}

class Program {
    static void Main() {
        string word = "Hi";
        System.Console.WriteLine(word.Repeat(3));
    }
}
AHiHi
BHi 3
CError: Extension method not found
DHiHiHi
Attempts:
2 left
💡 Hint
Think about what string.Concat and Enumerable.Repeat do together.
Predict Output
intermediate
2:00remaining
Output of extension method on int array
What is the output of this C# code that uses an extension method on an int array?
C Sharp (C#)
public static class ArrayExtensions {
    public static int SumPositive(this int[] arr) {
        int sum = 0;
        foreach (var n in arr) {
            if (n > 0) sum += n;
        }
        return sum;
    }
}

class Program {
    static void Main() {
        int[] numbers = { -1, 2, 3, -4, 5 };
        System.Console.WriteLine(numbers.SumPositive());
    }
}
A0
B5
C10
D-10
Attempts:
2 left
💡 Hint
Add only positive numbers in the array.
🔧 Debug
advanced
2:00remaining
Identify the error in this extension method
This extension method is intended to check if a string contains only digits. What error will this code cause when compiled?
C Sharp (C#)
public static class StringExtensions {
    public static bool IsDigitsOnly(string str) {
        foreach (char c in str) {
            if (!char.IsDigit(c)) return false;
        }
        return true;
    }
}

class Program {
    static void Main() {
        string s = "12345";
        System.Console.WriteLine(s.IsDigitsOnly());
    }
}
ACompile error: Extension method must have 'this' keyword on first parameter
BRuntime error: NullReferenceException
COutput: True
DOutput: False
Attempts:
2 left
💡 Hint
Check the method signature for extension methods.
📝 Syntax
advanced
2:00remaining
Which option correctly defines an extension method for List?
Which of the following options correctly defines an extension method named SumSquares that returns the sum of squares of all integers in a List?
Apublic static int SumSquares(this List<int> list) { int sum = 0; foreach(var x in list) sum += x * x; return sum; }
Bpublic int SumSquares(List<int> list) { int sum = 0; foreach(var x in list) sum += x * x; return sum; }
Cpublic static int SumSquares(List<int> list) { int sum = 0; foreach(var x in list) sum += x * x; return sum; }
Dpublic static int SumSquares(this List list) { int sum = 0; foreach(var x in list) sum += x * x; return sum; }
Attempts:
2 left
💡 Hint
Extension methods must be static and have 'this' before the first parameter with the exact type.
🚀 Application
expert
2:00remaining
Result of chaining extension methods on string
Given these extension methods, what is the output of the program?
C Sharp (C#)
public static class StringExtensions {
    public static string AddExclamation(this string s) => s + "!";
    public static string RepeatTwice(this string s) => s + s;
}

class Program {
    static void Main() {
        string greeting = "Hello";
        var result = greeting.AddExclamation().RepeatTwice();
        System.Console.WriteLine(result);
    }
}
AHelloHello!!
BHello!Hello!
CHello!Hello
DHello!!Hello!!
Attempts:
2 left
💡 Hint
Apply the methods step by step: first add exclamation, then repeat twice.