This is part 7 in a series of articles.

Prior to C## 8, if you add members to an interface, exiting code breaks if you do not implement the new members in any class that implements the interface.

As an example, consider the following interface definition:

This will compile without error. We could have multiple classes implementing this interface in the same project or across multiple projects.

What happens now if we wanted to add a new interface method that represents the ability to calculate a discount based on the customer’s previous order value and how long they have been a customer?

We could make the following change:

Now if we try and build, we’ll get the error: ‘Customer’ does not implement interface member

If we have multiple implementations of ICustomer, they will all break.

Default Interface Methods in C## 8

From C## 8 we can fix this problem by providing a default implementation of an interface method.

If we only had one implementation of ICustomer then we could go and add the implementation of CalculateLoyaltyDiscount. But if we had multiple implementations or we didn’t want to force a breaking change on existing implementers then we can actually add the implementation of the the method in the interface itself.

#icymi c# 8

 ICYMI C# 8 New Features: Upgrade Interfaces Without Breaking Existing Code
1.10 GEEK