Fluent Interfaces in C# – Extension Methods
In the previous post Iâve talked about what fluent interfaces is all about and gave a brief introduction to the subject â in this post weâll actually get to see some code.
The introduction you might not need
Extension methods enable you to ‘add’ methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type. For client code written in C# and Visual Basic, there is no apparent difference between calling an extension method and the methods that are actually defined in a type.
from C# Programming Guide
First introduced in .NET 3 â extension methods have been widely used to âaddâ methods to existing class without changing the actual class implementation. Using extension methods is as simple as creating a static method â in fact thatâs all it takes.
Using extension methods to create an API
The quick and simple way to create âpoor manâsâ fluent API is to use Extension methods.
All we need is to create a few static methods and we can transform the following code: // Instead of using this:
DateTime.Now.AddDays(14);
// Using extension method we can write:
DateTime.Now.AddWeeks(2);
// Or even this:
14.DaysFromNow();
The actual implementation is quite simple â but here it is just in case:
public static DateTime AddWeeks(this DateTime baseTime, int weeks)
{
return baseTime.AddDays(7 * weeks);
}
public static DateTime DaysFromNow(this int days)
{
return DateTime.Now.AddDays(days);
}
Thus by using simple method call we managed to transform â14â to âtwo weeksâ â not much but itâs a good place to start.
When to use
Extension methods are best used when the API weâre trying to add is on top some 3rd party component or internal class we do not wish to use.
This method is very effective when using along with âmethod chainingâ where using extension method is a good starting point for the whole API â but more on that in posts to come.
In the meantime keep in mind that one of the best fluent API out there (in my humble opinion) uses extension method extensively:
public static IEnumerable<TSource> Where<TSource> (this IEnumerable<TSource> source, Func<TSource, bool> predicate) public static IEnumerable<TResult> Select<TSource, TResult> (this IEnumerable<TSource> source, Func<TSource, TResult> selector)
Cons and pitfalls
There are some problems in âadding methodsâ to existing class â especially if this class is part of the CLR. In the example above Iâve used extension method on integer to create a new DateTime which has nothing to do with the previous type. Iâve added as method to every single integer in my system. Although I could filter the the use of the new method using namespace itâs still a lot of noise.
Conclusion
This is it â the first tool in your fluent interface belt. Experiment with it and use it wisely.
Reference: Fluent Interfaces in C# â Extension Methods from our NCG partner Dror Helper at the Helper Code blog.

