Default Interface Methods in C# 8.0

Background

We have been using the interfaces for years and we know that interfaces are just contracts and class that inherits them must implement all methods of interface.

Default Interface Methods in C# 8.0

Default Interface

Suppose this Interface is being implemented by multiple classes and now you want to add some more methods in that interface.

  1. If we talk about the time before C# 8.0 then you can not do this , because it will break all classes implementing that interface and you must implement that method.

  2. But with C# 8.0 now we can add default implementation of methods and it will not break all those classes that are implementing the interface.

Default Interface Methods in C# 8.0

Benefits

So what are the benefits of using it

  1. Without breaking default implementation we can add new methods now in interface , but we can do this through extension method as well ( creating an extension method for your interface).

  2. Class implementing interface is not aware about default implementation of method.

  3. Most importantly default interface methods can avoid diamond problem

This article was originally published at https://mwaseemzakir.substack.com/ on .

Whenever you're ready, there are 3 ways I can help you:

  1. Subscribe to my youtube channel : For in-depth tutorials, coding tips, and industry insights.
  2. Promote yourself to 9,000+ subscribers : By sponsoring this newsletter
  3. Patreon community : Get access to all of my blogs and articles at one place