C# › Extension Methods
How do you chain multiple extension methods together in a fluent style, and what design principle makes this possible?
IntermediateAnswer
As long as each extension method RETURNS a value of a type that has further applicable extension (or instance) methods, calls can be chained one after another — this is exactly the mechanism that makes LINQ's fluent query syntax (.Where().Select().OrderBy()) work.
public static class StringExtensions {
public static string Trimmed(this string s) => s.Trim();
public static string Capitalized(this string s) => char.ToUpper(s[0]) + s[1..];
}
string result = " hello world ".Trimmed().Capitalized(); // "Hello world"Real-world example Building a fluent, chainable string-processing or validation API similar in style to LINQ's own method chaining.