c# 8 default interface members feature

820 views c#
-3

c# 8 feature "default interface members" allows us to write something like:

interface ILogger
{
    void Log(LogLevel level, string message);
    void Log(Exception ex) => Log(LogLevel.Error, ex.ToString());
}

class ConsoleLogger : ILogger
{
    public void Log(LogLevel level, string message) {}
}

More info here

My questions are:

  • Isn't it a bad mixing? (I think, as a contract, an interface must be DECORATIVE ONLY)

  • Abstract classes already gives us the "half-implementation" functionality, so, why we need it?

  • How other languages deals with it?

answered question

1 Answer

5

  • Isn't it a bad mixing? (I think, as a contract, an interface must be DECORATIVE ONLY)

You probably think of interfaces like this because that's what they do in C#. The reasoning for this extension is given in the document you linked.

  • Abstract classes already gives us the "half-implementation" functionality, so, why we need it?

You can only inherit from one abstract class. Other reasons are also given in the document (Backwards compatible api changes)

  • How other languages deals with it?

I think that's also in the docs - the inspiration comes partly also from other languages

posted this

Have an answer?

JD

Please login first before posting an answer.