Extensions and interfaces together enable DRY code and mixin-like functionality

suggest change

Extension methods enable you to simplify your interface definitions by only including core required functionality in the interface itself and allowing you to define convenience methods and overloads as extension methods. Interfaces with fewer methods are easier to implement in new classes. Keeping overloads as extensions rather than including them in the interface directly saves you from copying boilerplate code into every implementation, helping you keep your code DRY. This in fact is similar to the mixin pattern which C# does not support.

System.Linq.Enumerable’s extensions to IEnumerable<T> is a great example of this. IEnumerable<T> only requires the implementing class to implement two methods: generic and non-generic GetEnumerator(). But System.Linq.Enumerable provides countless useful utilities as extensions enabling concise and clear consumption of IEnumerable<T>.

The following is a very simple interface with convenience overloads provided as extensions.

public interface ITimeFormatter
   string Format(TimeSpan span);

public static class TimeFormatter
    // Provide an overload to *all* implementers of ITimeFormatter.
    public static string Format(
        this ITimeFormatter formatter,
        int millisecondsSpan)
        => formatter.Format(TimeSpan.FromMilliseconds(millisecondsSpan));

// Implementations only need to provide one method. Very easy to
// write additional implementations.
public class SecondsTimeFormatter : ITimeFormatter
   public string Format(TimeSpan span)
       return $"{(int)span.TotalSeconds}s";

class Program
    static void Main(string[] args)
        var formatter = new SecondsTimeFormatter();
        // Callers get two method overloads!
        Console.WriteLine($"4500ms is rougly {formatter.Format(4500)}");
        var span = TimeSpan.FromSeconds(5);
        Console.WriteLine($"{span} is formatted as {formatter.Format(span)}");

Feedback about page:

Optional: your email if you want me to get back to you:

Table Of Contents