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

Extension method syntax 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 extension method call
What is the output of this C# program using an extension method?
C Sharp (C#)
using System;

public static class StringExtensions
{
    public static string AddExclamation(this string s)
    {
        return s + "!";
    }
}

class Program
{
    static void Main()
    {
        string greeting = "Hello";
        Console.WriteLine(greeting.AddExclamation());
    }
}
AHello!
BHello
CHello!!
DCompilation error
Attempts:
2 left
💡 Hint
Look at what the extension method does to the string.
❓ Predict Output
intermediate
2:00remaining
Extension method with value type
What is the output of this C# code using an extension method on an int?
C Sharp (C#)
using System;

public static class IntExtensions
{
    public static int Square(this int x)
    {
        return x * x;
    }
}

class Program
{
    static void Main()
    {
        int number = 4;
        Console.WriteLine(number.Square());
    }
}
ACompilation error
B8
C4
D16
Attempts:
2 left
💡 Hint
The extension method returns the number multiplied by itself.
🔧 Debug
advanced
2:00remaining
Identify the error in extension method syntax
Which option contains the syntax error in defining an extension method?
C Sharp (C#)
public static class Extensions
{
    public static int Double(int x)
    {
        return x * 2;
    }
}
AThe method cannot be static inside a static class.
BThe method must be static and have 'this' keyword before the first parameter type.
CThe method must return void for extension methods.
DThe method name must start with 'Ext' for extension methods.
Attempts:
2 left
💡 Hint
Check the method parameter for extension method syntax.
🧠 Conceptual
advanced
2:00remaining
Extension method resolution priority
Given a class with an instance method and an extension method with the same name and signature, which method is called when invoked on an instance?
ABoth methods are called sequentially.
BThe extension method is called, instance method is ignored.
CThe instance method is called, extension method is ignored.
DCompilation error due to ambiguity.
Attempts:
2 left
💡 Hint
Instance methods have higher priority than extension methods.
❓ Predict Output
expert
3:00remaining
Output of chained extension methods
What is the output of this C# program using chained extension methods on a string?
C Sharp (C#)
using System;

public static class StringExtensions
{
    public static string AddPeriod(this string s)
    {
        return s + ".";
    }

    public static string ToUpperCase(this string s)
    {
        return s.ToUpper();
    }
}

class Program
{
    static void Main()
    {
        string text = "hello";
        string result = text.AddPeriod().ToUpperCase();
        Console.WriteLine(result);
    }
}
AHELLO.
Bhello.
CHELLO
Dhello
Attempts:
2 left
💡 Hint
Check the order of method calls and their effects on the string.

Practice

(1/5)
1. What is the main purpose of an extension method in C#?
easy
A. To override existing methods in a class
B. To create new classes that inherit from existing ones
C. To add new methods to existing types without modifying their source code
D. To define constructors for existing classes

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 with other options

    Options B, C, and D describe inheritance, overriding, and constructors, which are different concepts.
  3. Final Answer:

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

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

Solution

  1. Step 1: Check extension method declaration rules

    Extension methods must be static methods inside static classes, and the first parameter must have the 'this' keyword before the type.
  2. Step 2: Analyze each option

    public static void MyExtension(this string str) { } correctly uses 'public static void' and 'this string str'. public void MyExtension(string this str) { } misplaces 'this'. static public void MyExtension(string str) { } misses 'this'. public static void MyExtension(string str) { } misses 'this' keyword.
  3. Final Answer:

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

    Extension method syntax requires 'this' before first parameter [OK]
Hint: Remember 'this' before first parameter in static method [OK]
Common Mistakes:
  • Omitting 'this' keyword before the first parameter
  • Placing 'this' after the parameter type
  • Not making the method static
3. What will be the output of the following code?
public static class StringExtensions {
    public static int WordCount(this string str) {
        return str.Split(' ').Length;
    }
}

string sentence = "Hello world from C#";
int count = sentence.WordCount();
Console.WriteLine(count);
medium
A. 4
B. 1
C. 0
D. Compilation error

Solution

  1. Step 1: Understand the extension method behavior

    The method WordCount splits the string by spaces and returns the number of parts.
  2. Step 2: Count words in the string

    The string "Hello world from C#" has 4 words separated by spaces, so the method returns 4.
  3. Final Answer:

    4 -> Option A
  4. Quick Check:

    Word count of "Hello world from C#" = 4 [OK]
Hint: Count words by splitting string on spaces [OK]
Common Mistakes:
  • Forgetting to make the extension method static
  • Assuming Split returns empty array for non-empty string
  • Expecting compilation error due to extension method
4. Identify the error in the following extension method code:
public static class IntExtensions {
    public static bool IsEven(int number) {
        return number % 2 == 0;
    }
}
medium
A. The method is not static
B. The class is not static
C. The method should return int instead of bool
D. The first parameter is missing the 'this' keyword

Solution

  1. Step 1: Check extension method parameter syntax

    Extension methods require the first parameter to have the 'this' keyword before the type to indicate the extended type.
  2. Step 2: Analyze the code

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

    The first parameter is missing the 'this' keyword -> Option D
  4. Quick Check:

    Extension method needs 'this' before first parameter [OK]
Hint: Extension method's first parameter must start with 'this' [OK]
Common Mistakes:
  • Forgetting 'this' keyword on first parameter
  • Making method non-static
  • Not declaring class as static
5. You want to create an extension method IsNullOrEmpty for string that returns true if the string is null or empty. Which of the following is the correct implementation?
hard
A. public static bool IsNullOrEmpty(this string str) { return str == null || str == ""; }
B. public bool IsNullOrEmpty(string str) { return str == null || str == ""; }
C. public static bool IsNullOrEmpty(string str) { return str == null || str == ""; }
D. public static bool IsNullOrEmpty(this string str) { return str.Length == 0; }

Solution

  1. Step 1: Check extension method signature

    The method must be static and have 'this' before the first parameter type to be an extension method.
  2. Step 2: Verify method logic

    public static bool IsNullOrEmpty(this string str) { return str == null || str == ""; } correctly checks if the string is null or empty. public static bool IsNullOrEmpty(this string str) { return str.Length == 0; } misses null check, which causes error if str is null.
  3. Final Answer:

    public static bool IsNullOrEmpty(this string str) { return str == null || str == ""; } -> Option A
  4. Quick Check:

    Extension method with 'this' and null check is correct [OK]
Hint: Check null before accessing string properties in extension [OK]
Common Mistakes:
  • Omitting 'this' keyword on first parameter
  • Not checking for null before accessing Length
  • Making method non-static
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