Proposed Default Interface Methods In C# 8

There is a current proposal that’s getting traction (By some) to make it into C# 8. That’s “default interface methods”. Because at the time of writing, C# 8 is not out, nor has this new language feature even “definitely” made it into that release, this will be more about the proposal and my two cents on it rather than any specific tutorial on using them.

What Are Default Interface Methods?

Before I start, it’s probably worth reading the summary of the proposal on Github here : It’s important to remember is that this is just a proposal so not everything described will actually be implemented (Or implemented in the way it’s initially being described). Take everything with a grain of salt.

The general idea is that an interface can provide a method body. So :

interface IMyService
    string HelloWorld() { return "Hello World"; }

A class that implements this interface does not have to implement methods where a body has been provided. So for example :

class MyService : IMyService
    // Absolutely no body required.

IMyService myService = new MyService();
var helloWorld = myService.HelloWorld(); //Returns the string "Hello World"

The interesting thing is that the class itself does not have the ability to run methods that have been defined and implemented on an interface. So :

MyService myService = new MyService();
var helloWorld = myService.HelloWorld(); //Error
//But if we cast to the interface we are good to go
var helloWorld2 = ((IMyService)myService).HelloWorld();

The general idea is that this will now allow you to do some sort of multiple inheritance of behaviour/methods (Which was previously unavailable in C#).

interface IMySecondInterface
    string HelloWorld { return "Hello World 2";}

class MyService : IMyService, IMySecondInterface
var myService = new MyService();
var helloWorld = ((IMyService)myService).HelloWorld(); // Returns Hello World
helloWorld = ((IMySecondInterface)myService).HelloWorld(); // Return Hello World 2

There are a few other things that then are required (Or need to become viable) when opening this up. For example allowing private level methods with a body inside an interface (To share code between default implementations).

Abstract Classes vs Default Interface Methods

While it does start to blur the lines a bit, there are still some pretty solid differences. The best quote I heard about it was :

Interfaces define behaviour, classes define state.

And that does make some sense. Interfaces still can’t define a constructor, so if you want to share constructor logic, you will need to use an abstract/base class. An interface also cannot define class level variables/fields.

Classes also have the ability to define accessibility of it’s members (For example making a method protected), whereas with an interface everything is public. Although part of the proposal is extending interfaces with things like the static keyword and protected, internal etc (I really don’t agree with this).

Because the methods themselves are only available when you cast back to the interface, I can’t really see it being a drop in replacement for abstract classes (yet), but it does blur the lines just enough to ask the question.

My Two Cents

This feels like one of those things that just “feels” wrong. And that’s always a hard place to start because it’s never going to be black and white. I feel like interfaces are a very “simple” concept in C# and this complicates things in ways which I don’t see a huge benefit. It reminds me a bit of the proposal of “Primary Constructors” that was going to make it into C# 6 (See more here : Thankfully that got dumped but it was bolting on a feature that I’m not sure anyone was really clamoring for.

But then again, there are some merits to the conversation. One “problem” with interfaces is that you have to implement every single member. This can at times lock down any ability to extend the interface because you will immediately blow up any class that has already inherited that interface (Or it means your class becomes littered with throw new NotImplementedException() ).

There’s even times when you implement an interface for the first time, and you have to pump out 20 or so method implementations that are almost boilerplate in content. A good example given in the proposal is that of IEnumerable. Each time you implement this you are required to get RSI on your fingers by implementing every detail. Where if there was default implementations, there might be only a couple of methods that you truly want to make your own, and the default implementations do the job just fine for everything else.

All in all, I feel like the proposal should be broken down and simplified. It’s almost like an American political bill in that there seems to be a lot in there that’s causing a bit of noise (allowing static, protected members on interfaces etc). It needs to be simplified down and just keep the conversation on method bodies in interfaces and see how it works out because it’s probably a conversation worth having.

What do you think? Drop a comment below.

6 thoughts on “Proposed Default Interface Methods In C# 8”

  1. Oh, this feels nasty.
    The only supporting argument seems to be that multiple inheritance would be supported. But, repurposing interfaces to achieve that feels very wrong. I’m sure if the c# team really wanted to, they’d find a more natural way to do it.

  2. Definitely feels wrong. I don’t want this and I definitely don’t want to deal with someone else’s code that uses this!

  3. Something similar can already be achieved using extension methods so this is just a combination of both concepts.

  4. I think the main reason for this feature is backward compatibility – if there is a new method to an interface for example, you don’t have to worry about 3rd party components that implement that interface, but were not updated to the latest version.
    But imho that just feels wrong and can lead to unexpected behavior.

  5. Grotesque. Give people enough rope to hang themselves and they will. You’re going to turn the language into a joke if you’re not careful.

  6. I am very confused…
    They are really going to change a very basic and fundamental concept of OOP!!!!
    Why should an interface has default implementation for method?????
    In OOP an interface is a contract for class that assigns structure of it.
    In fact because of this reason that interface doesn’t have implementation, classes can implement(inherit) from multiple interfaces.
    This is very mess….
    With this feature SOLID principles are going to die and Abstract class will be inefficient….


Leave a Comment