Interface + Extension (mixin) vs Base Class

.net, abstract-class, extension-methods, interface

Solution

Downside of extension methods: clients pre-C#3/VB9 won't be able to use it as easily.

That's about it as far as I'm concerned - I think the interface-based approach is significantly nicer. You can then mock out your dependencies nicely, and everything is basically less tightly coupled. I'm not a huge fan of class inheritance unless it's really about specialization :)

EDIT: I've just thought of one other benefit which might be relevant. It's possible that some of the concrete implementations could provide more optimized versions of some of the general methods.

`Enumerable.Count` is a good example of this - it explicitly checks whether the sequence implements `IList` or not, because if it does it can call `Count` on the list instead of iterating through the whole sequence. If `IEnumerable<T>` had been an abstract class with a virtual `Count()` method, it could have been overridden in `List<T>` rather than there being a single implementation which knows about `IList` explicitly. I'm not saying this is always relevant, nor that `IEnumerable<T>` should have been an abstract class (definitely not!) - just pointing it out as a small possible disadvantage. That's where polymorphism really would be appropriate, by specializing existing behaviour (admittedly in a way which only affects performance instead of the result).

Problem

Is an interface + extension methods (mixin) preferable to an abstract class? If your answer is "it depends", what does it depend upon? I see two possible advantages to the interface + extension approach. - Interfaces are multiply inheritable and classes are not. - You can use extension methods to extend interfaces in a non-breaking way. (Clients that implement your interface will gain your new base implementation but still be able to override it.) I have not yet thought of a downside to this approach. There may be a glaringly simple reason that the interface + extension approach will fail. Two helpful articles on this topic are - Create Mixins with Interfaces and Extension Methods - Abstract Base Classes Have Versioning Problems Too

Original source