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

Extension method syntax 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 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.