Default implementations in interfaces

Posted by Anuraj on Tuesday, May 14, 2019 Reading time :1 minute

C# 8.0 C#

This post is about the new C# 8.0 feature Default implementations in interfaces. This feature helps you to provide an implementation for your new method in an interface. So you won’t break existing implementation by adding a new method. From the documentation - An interface member can now be specified with a code body, and if an implementing class or struct does not provide an implementation of that member, no error occurs. Instead, the default implementation is used.

To work with default interface implementation feature, you have to use VS 2019 and you need to choose the C# 8.0 version from Advanced Build Settings.

Enable C# 8.0 features

Once you enable it, you use the default implementations feature.

interface ILogger
{
    void Log(string message);
}

class ConsoleLogger : ILogger
{
    public void Log(string message)
    {
        Console.WriteLine(message);
    }
}

So in the code, I am using a interface ILogger with one method Log(). And there is a ConsoleLogger implementation, which will log the details to the console window. In earlier versions on C#, if I add a method in ILogger, like this. I have added a method which will log exceptions as well.

interface ILogger
{
    void Log(string message);
    void Log(Exception exception);
}

It will break all the existing implementations.

Method not implemented error

This behaviour is same in C# 8.0 as well, but as alternative you can provide a default implementation in the interface like this.

interface ILogger
{
    void Log(string message);
    void Log(Exception exception) => Console.WriteLine(exception);
}

Now if you build the project, Visual Studio will build the project without showing any error. And if required, the implementation classes can implement the new method as well, if the method implemented in the classes, runtime will execute the implemented methods - it will ignore the default implementation, otherwise runtime will use the default implementation.

Happy Programming :)

What do you think? I would like to hear your thoughts, suggestions, and questions in the comments section below.



Did you like this article? Share it with your friends

Facebook Twitter Google+ LinkedIn Reddit StumbleUpon



Translate
Subscribe
BMC logoBuy me a coffee


Copyright © 2019 - Anuraj P. Blog content licensed under the Creative Commons CC BY 2.5 | Unless otherwise stated or granted, code samples licensed under the MIT license. This is a personal blog. The opinions expressed here represent my own and not those of my employer. Hosted with ❤ by GitHub