Bird
Raised Fist0
C Sharp (C#)programming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of an extension method in C#?
easy
A. To add new methods to existing types without modifying their source code
B. To create a new class that inherits from a built-in type
C. To override existing methods in built-in types
D. To replace built-in types with custom implementations

Solution

  1. Step 1: Understand extension method purpose

    Extension methods allow adding new functionality to existing types without changing their original code.
  2. Step 2: Compare options

    Replacing built-in types with custom implementations, creating a new class that inherits from a built-in type, or overriding existing methods are different concepts from extension methods.
  3. Final Answer:

    To add new methods to existing types without modifying their source code -> Option A
  4. Quick Check:

    Extension methods add new actions without changing original types [OK]
Hint: Extension methods add features without changing original code [OK]
Common Mistakes:
  • Confusing extension methods with inheritance
  • Thinking extension methods override existing methods
  • Believing extension methods replace built-in types
2. Which of the following is the correct syntax to declare an extension method for the built-in string type?
easy
A. public void MyMethod(string this str) { }
B. public static void MyMethod(this string str) { }
C. static void MyMethod(string str) { }
D. public static void MyMethod(string str) { }

Solution

  1. Step 1: Recall extension method syntax

    Extension methods must be static methods inside static classes, with this before the first parameter type.
  2. Step 2: Check options

    Only public static void MyMethod(this string str) { } uses this string str correctly. The other options miss the this keyword or static modifier.
  3. Final Answer:

    public static void MyMethod(this string str) { } -> Option B
  4. Quick Check:

    Extension method syntax requires 'this' before first parameter [OK]
Hint: Look for 'this' keyword before first parameter in static method [OK]
Common Mistakes:
  • Omitting the 'this' keyword before the first parameter
  • Not making the method static
  • Placing 'this' after the parameter name
3. Given the following code, what will be the output?
public static class StringExtensions {
    public static bool IsCapitalized(this string s) {
        return !string.IsNullOrEmpty(s) && char.IsUpper(s[0]);
    }
}

class Program {
    static void Main() {
        string word = "Hello";
        Console.WriteLine(word.IsCapitalized());
    }
}
medium
A. True
B. False
C. Compilation error
D. Runtime exception

Solution

  1. Step 1: Understand the extension method logic

    The method checks if the string is not null or empty and if the first character is uppercase.
  2. Step 2: Analyze the input string

    The string "Hello" starts with 'H', which is uppercase, so the method returns true.
  3. Final Answer:

    True -> Option A
  4. Quick Check:

    First letter uppercase in "Hello" means IsCapitalized() returns True [OK]
Hint: Check if first character is uppercase and string is not empty [OK]
Common Mistakes:
  • Assuming method returns false for capitalized words
  • Confusing null or empty check logic
  • Expecting compilation error due to extension method
4. Identify the error in this extension method code:
public static class IntExtensions {
    public static bool IsEven(int number) {
        return number % 2 == 0;
    }
}
medium
A. The method must be inside a non-static class
B. The method cannot return bool
C. The method must be static and have 'this' before the first parameter
D. The method name cannot be 'IsEven'

Solution

  1. Step 1: Check extension method requirements

    Extension methods must be static and the first parameter must have the 'this' keyword to specify the type being extended.
  2. Step 2: Identify missing 'this' keyword

    The method parameter 'int number' lacks 'this', so it is not a valid extension method.
  3. Final Answer:

    The method must be static and have 'this' before the first parameter -> Option C
  4. Quick Check:

    Extension methods require 'this' keyword on first parameter [OK]
Hint: Extension method first parameter must have 'this' keyword [OK]
Common Mistakes:
  • Forgetting 'this' before the first parameter
  • Placing method in non-static class
  • Using instance methods instead of static
5. You want to create an extension method for List<int> that returns the sum of all even numbers in the list. Which of the following implementations correctly achieves this?
hard
A. public static int SumEven(this List<int> list) { return list.Sum(n => n % 2); }
B. public int SumEven(List<int> list) { return list.Where(n => n % 2 == 0).Sum(); }
C. public static int SumEven(List<int> list) { return list.Sum(n => n % 2 == 0); }
D. public static int SumEven(this List<int> list) { int sum = 0; foreach(var n in list) { if(n % 2 == 0) sum += n; } return sum; }

Solution

  1. Step 1: Verify extension method signature

    public static int SumEven(this List<int> list) { int sum = 0; foreach(var n in list) { if(n % 2 == 0) sum += n; } return sum; } and the version using list.Sum(n => n % 2) have 'this List<int> list' parameter, making them extension methods. The others lack 'this'.
  2. Step 2: Check logic correctness

    public static int SumEven(this List<int> list) { int sum = 0; foreach(var n in list) { if(n % 2 == 0) sum += n; } return sum; } sums even numbers correctly using a loop and condition. public static int SumEven(this List<int> list) { return list.Sum(n => n % 2); } sums n % 2 which returns 0 or 1, not the number itself. The others are not extension methods.
  3. Final Answer:

    public static int SumEven(this List<int> list) { int sum = 0; foreach(var n in list) { if(n % 2 == 0) sum += n; } return sum; } -> Option D
  4. Quick Check:

    Extension method with correct sum logic is public static int SumEven(this List<int> list) { int sum = 0; foreach(var n in list) { if(n % 2 == 0) sum += n; } return sum; } [OK]
Hint: Use 'this' and sum only even numbers correctly [OK]
Common Mistakes:
  • Missing 'this' keyword for extension method
  • Incorrect lambda in Sum causing wrong results
  • Not filtering even numbers before summing
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard