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

Fluent interface with extensions in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Fluent Interface Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a simple fluent interface chain

What is the output of this C# code using a fluent interface?

C Sharp (C#)
public class Builder {
    private string text = "";
    public Builder AddHello() {
        text += "Hello";
        return this;
    }
    public Builder AddSpace() {
        text += " ";
        return this;
    }
    public Builder AddWorld() {
        text += "World";
        return this;
    }
    public override string ToString() => text;
}

var result = new Builder().AddHello().AddSpace().AddWorld().ToString();
System.Console.WriteLine(result);
AHello World
BHelloWorld
CdlroW olleH
DHello World
Attempts:
2 left
💡 Hint

Look at how spaces are added in the chain.

🧠 Conceptual
intermediate
1:30remaining
Purpose of extension methods in fluent interfaces

Why are extension methods useful when building fluent interfaces in C#?

AThey automatically generate UI elements for the fluent interface.
BThey allow adding new chainable methods to existing classes without modifying them.
CThey prevent the fluent interface from being used outside the assembly.
DThey convert synchronous methods into asynchronous ones.
Attempts:
2 left
💡 Hint

Think about how you can add methods to a class you cannot change.

🔧 Debug
advanced
2:00remaining
Identify the error in this fluent interface extension method

What error will this code cause when compiling?

public static class BuilderExtensions {
    public static Builder AddExclamation(this Builder builder) {
        builder.ToString() += "!";
        return builder;
    }
}
ACannot assign to method call result (ToString() returns string, which is immutable).
BExtension methods cannot return the original object.
CMissing semicolon after return statement.
DBuilder class must be static to use extension methods.
Attempts:
2 left
💡 Hint

Consider what ToString() returns and if it can be assigned to.

📝 Syntax
advanced
2:00remaining
Correct syntax for a fluent extension method

Which option shows the correct syntax for a fluent extension method that adds a period at the end of the Builder text?

Astatic Builder AddPeriod(Builder builder) { builder.text += "."; return builder; }
Bpublic Builder AddPeriod(this Builder builder) { builder.text += "."; return builder; }
Cpublic static Builder AddPeriod(this Builder builder) { builder.text += "."; return builder; }
Dpublic static void AddPeriod(this Builder builder) { builder.text += "."; }
Attempts:
2 left
💡 Hint

Remember extension methods must be static and inside static classes.

🚀 Application
expert
3:00remaining
Result of chaining multiple fluent extension methods

Given the following code, what is the final output?

public class Builder {
    private string text = "Start";
    public Builder Append(string s) {
        text += s;
        return this;
    }
    public override string ToString() => text;
}

public static class BuilderExtensions {
    public static Builder AddDash(this Builder b) => b.Append("-");
    public static Builder AddStar(this Builder b) => b.Append("*");
    public static Builder AddPlus(this Builder b) => b.Append("+");
}

var result = new Builder()
    .AddDash()
    .AddStar()
    .AddPlus()
    .AddDash()
    .ToString();
System.Console.WriteLine(result);
AStart-*+-
BStart-+*-
CStart-*+
DStart-*-+
Attempts:
2 left
💡 Hint

Follow the chain step by step, appending each symbol in order.