2

Dear editors, Why you mark this question as Opinion-based? I have no opinion, I just asked a basic question.

I have a basic confusion with C# 8 Interface implementation concept, As you know Interface is not more than a Contract, Why we need to default implementation? Is it correct conceptually? Why a Contract must have an Implementation?

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
Saeid Babaei
  • 481
  • 2
  • 16
  • 2
    https://learn.microsoft.com/en-us/dotnet/csharp/tutorials/default-interface-methods-versions – Pavel Anikhouski Dec 23 '19 at 14:09
  • @PavelAnikhouski Thank you, But I am looking for conceptual correctness not only technical requirements. – Saeid Babaei Dec 23 '19 at 14:15
  • We don't "need" it as we lived through 7 versions or C# without it. It's an efficiency gain as you don't have to have a base class to provide a "default" implementation. – D Stanley Dec 23 '19 at 14:18
  • 1
    As Eric Lippert has commented in the past, the purpose of C# is to allow working programmers to create working software. Not to be "conceptually pure", if that's at the expense of convenience. (With apologies to Mr Lippert for my paraphrasing) – Damien_The_Unbeliever Dec 23 '19 at 14:18
  • 1
    you can achieve this by abstract class but then you loose multiple inheritances. so if you need two abstract class inheritance then you can do by these. – divyang4481 Dec 23 '19 at 15:56
  • @divyang4481Thanks. This is more close to my opinion. – Saeid Babaei Dec 24 '19 at 07:02
  • 1
    @SaeidBabaei you're asking for *opinions* based on *your* interpretation of interfaces.Which are *not* just contracts. That's why the question was closed. In any case, the question has been asked before with *different* answers each time - because `is it correct` is definitely asking for an opinion. The tag's description explains why DIMs were added and why they are actually *necessary* in one of the hottest .NET Core markets - Android – Panagiotis Kanavos Dec 24 '19 at 11:26
  • In fact, each person's opinion depends on their background. People with experience on JavaScript, Java or PHP are already familiar with traits so DIMs won't appear unusual. People who only worked with .NET will wonder what Mads Torgersen was drinking when he approved DIMs. – Panagiotis Kanavos Dec 24 '19 at 11:29
  • See [default interface methods](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-8.0/default-interface-methods) for a glimpse of what they were thinking. – Theraot Dec 24 '19 at 11:37

1 Answers1

0

Suppose we inherited from an interface in different places if we wanted to add a new member and we don’t want to affect any existing class with this change. This is how we define interface with current version of C# and we are stuck. In C# 8.0 we can solve the problem by providing implementation to method.

Default implementations is powerful language feature coming to C# 8.0. Although it may seem dangerous for some developers then others will certainly be happy with it. Those who are writing libraries and components for public use may find default implementations specially useful as they let us avoid breaking changes in interfaces.

Document

Reza Jenabi
  • 3,884
  • 1
  • 29
  • 34