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

Fluent interface with extensions in C Sharp (C#) - Mini Project: Build & Apply

Choose your learning style9 modes available
Fluent interface with extensions
📖 Scenario: Imagine you are building a simple text formatter for a chat application. You want to create a way to style messages by chaining methods like Bold(), Italic(), and Underline() in a smooth, readable way.
🎯 Goal: Build a fluent interface using extension methods that allows chaining text styling methods on a string. The final output should show the styled text with tags representing the styles.
📋 What You'll Learn
Create a class called TextFormatter that holds a string value.
Create extension methods Bold(), Italic(), and Underline() for TextFormatter.
Each extension method should wrap the current text with corresponding tags: <b>, <i>, and <u> respectively.
Allow chaining of these extension methods to apply multiple styles.
Add a method GetText() to return the final styled string.
💡 Why This Matters
🌍 Real World
Fluent interfaces are common in libraries and frameworks to make code easier to read and write, such as building queries, configuring settings, or styling text.
💼 Career
Understanding fluent interfaces and extension methods is valuable for C# developers, especially when working with APIs like LINQ, Entity Framework, or UI frameworks.
Progress0 / 4 steps
1
Create the TextFormatter class
Create a public class called TextFormatter with a public string property called Text. Add a constructor that takes a string parameter called text and sets the Text property.
C Sharp (C#)
Need a hint?

Remember to create a constructor that sets the Text property from the parameter.

2
Add the Bold extension method
Create a public static class called TextFormatterExtensions. Inside it, create a public static method called Bold that takes a TextFormatter parameter named formatter with the this keyword. The method should wrap formatter.Text with <b> and </b> tags, update formatter.Text, and return the formatter object.
C Sharp (C#)
Need a hint?

Use this TextFormatter formatter as the first parameter to make it an extension method.

3
Add Italic and Underline extension methods
Inside the TextFormatterExtensions class, add two more public static extension methods: Italic and Underline. Both take a TextFormatter parameter named formatter with this. Italic should wrap formatter.Text with <i> and </i>. Underline should wrap formatter.Text with <u> and </u>. Both methods update formatter.Text and return formatter.
C Sharp (C#)
Need a hint?

Follow the same pattern as Bold for both Italic and Underline.

4
Use the fluent interface and print the result
In the Main method, create a new TextFormatter object with the text "Hello". Chain the extension methods Bold(), Italic(), and Underline() in that order. Then print the final Text property of the object using Console.WriteLine.
C Sharp (C#)
Need a hint?

Remember to create the TextFormatter object first, then chain the extension methods, and finally print the Text property.