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

Fluent interface with extensions in C Sharp (C#) - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a fluent method that returns the same object.

C Sharp (C#)
public class Builder {
    public Builder SetName(string name) {
        // set name logic
        return [1];
    }
}
Drag options to blanks, or click blank then click option'
Athis
Bname
CBuilder
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Returning the parameter 'name' instead of the object.
Returning null which breaks the chain.
2fill in blank
medium

Complete the extension method to add a fluent method to the Builder class.

C Sharp (C#)
public static class BuilderExtensions {
    public static Builder [1](this Builder builder, int age) {
        // set age logic
        return builder;
    }
}
Drag options to blanks, or click blank then click option'
ASetAge
BAddAge
CWithAge
DAge
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method name that does not clearly indicate its purpose.
Omitting the 'this' keyword in the parameter.
3fill in blank
hard

Fix the error in the extension method to correctly return the builder for chaining.

C Sharp (C#)
public static Builder SetHeight(this Builder builder, double height) {
    builder.Height = height;
    return [1];
}
Drag options to blanks, or click blank then click option'
Aheight
Bthis
Cbuilder
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Returning the property value instead of the builder.
Returning 'this' inside a static method which is invalid.
4fill in blank
hard

Fill both blanks to create a fluent extension method that sets a string property and returns the builder.

C Sharp (C#)
public static Builder [1](this Builder builder, string city) {
    builder.[2] = city;
    return builder;
}
Drag options to blanks, or click blank then click option'
ASetCity
BCity
CCityName
DSetLocation
Attempts:
3 left
💡 Hint
Common Mistakes
Using the property name as the method name.
Setting a property that does not exist.
5fill in blank
hard

Fill all three blanks to create a fluent extension method that sets an integer property with validation and returns the builder.

C Sharp (C#)
public static Builder [1](this Builder builder, int score) {
    if (score [2] 0) {
        builder.[3] = score;
    }
    return builder;
}
Drag options to blanks, or click blank then click option'
ASetScore
B>
CScore
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong comparison operator.
Setting a property with a different name.