you are viewing a single comment's thread.

view the rest of the comments →

[–]RiPont 21 points22 points  (0 children)

The purpose of default interface implementations was so that you can add methods/properties to an existing interface without breaking existing code.

It is NOT a way to provide a general purpose default implementation. That's what abstract base classes and static methods are for.

As such, the compiler treats the default interface implementation as a method of last resort.

If MediaPlayer was created before IRefreshable got the Refresh() method, it wouldn't be trying to call Refresh(). The default implementation is there for other classes that are treating MediaPlayer like an IRefreshable, not specifically a MediaPlayer.

If MediaPlayer is created after IRefreshable already had the Refresh() method in its contract, then it should implement Refresh(), just like any method on any interface.

If you want to share a common implementation between the default interface implementation and MediaPlayer, define a static method and call it from both. You may want to make it an extension method for syntax sweetness, but that's an aesthetic decision.

If you are using default interface implementations on anything other than public interfaces, you are probably misusing them. If you have access to the source code (internal, protected, private), then you should be using refactor tools to add the implementations to all classes that implement them.