Extension methods and Interfaces.

Posted Thu, Jan 15 2009 14:58 by bill

A comment by Joe Duffy on the Framework Guidelines for Extension says :

Extension methods can also be used to provide actual concrete method implementations for interfaces.

And this is a very important point. In .NET there is no support for multiple implementation inheritance. So although you could use an abstract class (MustInherit) with some implementation in it, you would be tied to that single chain in your class inheritance. Interfaces on the other hand are extremely flexible, supporting multiple inheritance both in interfaces and implementing classes.  So by providing extension methods for interfaces, you get great flexibility and code re-use, and you also get to extend existing interfaces.. the classic example being the Enumerable set of extensions which extend IEnumerable(Of T)

Filed under: , , ,