| Muhammad Waseem

About Newsletter
Books
30 .NET Tips
Sponsorship
Default Interface Methods in C# 8.0
Mar 11, 2023
3 min read

Default Interface Methods in C# 8.0

Sponsor this Newsletter

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.

public interface INewsletter
{
    void WriteFirstNewsletter();
}

public class Newsletter : INewsletter
{
    public void WriteFirstNewsletter()
    {
        Console.WriteLine("I am first Newsletter");
    }
}

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.

public interface INewsletter
{
    void WriteFirstNewsletter();
    
    void WriteLastNewsletter()
    {
        Console.WriteLine("I am last Newsletter {By Default}");
    }
}

public class Newsletter : INewsletter
{
    public void WriteFirstNewsletter()
    {
        Console.WriteLine("I am first Newsletter");
    }
}

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 Mar 11, 2023 .

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
Previous Next

Subscribe to Newsletter

Join 9,000 Software Engineers

Buy Me a Coffee

Enjoy my articles? Support me by buying a coffee!

Buy Me a Coffee

Muhammad Waseem

Resources
  • Books
  • Courses
Newsletter
  • Articles
  • Sponsorship
Books
  • 30 .NET Tips
  • 100 .NET Tips (Soon)
Author
  • About Us
  • Contact Us
Policy
  • Privacy Policy
  • Terms and Conditions
Interview
  • C# & .NET
  • Web API

Join my .NET newsletter and stay updated!

© 2025 Muhammad Waseem. All rights reserved.