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

Why extension methods are needed in C Sharp (C#) - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Extension Method Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of extension method usage

What is the output of this C# code using an extension method?

C Sharp (C#)
using System;

public static class StringExtensions
{
    public static string AddHello(this string str)
    {
        return "Hello, " + str;
    }
}

class Program
{
    static void Main()
    {
        string name = "Alice";
        Console.WriteLine(name.AddHello());
    }
}
AHello, Alice
BAliceHello,
CCompile error: extension method not found
DHello Alice
Attempts:
2 left
💡 Hint

Extension methods add new methods to existing types without changing them.

🧠 Conceptual
intermediate
1:30remaining
Why use extension methods?

Which of the following best explains why extension methods are needed in C#?

ATo allow methods to be called without an instance of a class
BTo replace inheritance and polymorphism completely
CTo add new methods to existing types without modifying their source code or creating a new derived type
DTo improve runtime performance by inlining methods
Attempts:
2 left
💡 Hint

Think about how you can add functionality to classes you don't own.

🔧 Debug
advanced
2:00remaining
Identify the error with extension method usage

What error will this code produce?

C Sharp (C#)
using System;

public static class Extensions
{
    public static int Double(this int x)
    {
        return x * 2;
    }
}

class Program
{
    static void Main()
    {
        int number = 5;
        Console.WriteLine(number.Double());
        Console.WriteLine(Extensions.Double(number));
    }
}
AOutputs 10 twice
BCompile error: Cannot call extension method as static method
CRuntime error: NullReferenceException
DCompile error: Extension method must be in a namespace
Attempts:
2 left
💡 Hint

Extension methods can be called like instance methods or static methods with the instance as argument.

📝 Syntax
advanced
1:30remaining
Which code correctly defines an extension method?

Which option shows the correct syntax for defining an extension method in C#?

Apublic int Square(this int x) { return x * x; }
Bpublic static int Square(this int x) { return x * x; }
Cstatic int Square(int this x) { return x * x; }
Dpublic static int Square(int x) this { return x * x; }
Attempts:
2 left
💡 Hint

Extension methods must be static and have 'this' before the first parameter.

🚀 Application
expert
3:00remaining
Using extension methods to add functionality to a sealed class

Given that the string class is sealed and cannot be inherited, how can extension methods help add a method IsPalindrome to check if a string reads the same backward?

Which code correctly implements this?

Apublic static bool IsPalindrome(this string s) { return s == s.Reverse(); }
Bpublic bool IsPalindrome(string s) { return s == new string(s.Reverse().ToArray()); }
Cpublic static bool IsPalindrome(string s) { return s == new string(s.Reverse().ToArray()); }
Dpublic static bool IsPalindrome(this string s) { return s == new string(s.Reverse().ToArray()); }
Attempts:
2 left
💡 Hint

Remember extension methods need this on the first parameter and must return a bool.