Default Implementation in interfaces | Sololearn: Learn to code for FREE!

+3

Default Implementation in interfaces

In the c# course it is given that all members of interface are abstract by default so then we should not be able to implement an abstract method inside the base( as abstract methods can't be defined inside the base class) ....then how is default implementation possible ??! Like in the code Finish() method ( which is abstract by default ) is used. Code = https://code.sololearn.com/c3a13A92A61A

5/7/2021 2:38:02 PM

Shubham Yadav

6 Answers

New Answer

+7

Shubham Yadav This is a really good question. Consider the following methods in an interface: interface IFoo { void AbstractMethod(); void DefaultMethod() { // Contains a method body } } The IFoo.AbstractMethod() doesn't have a method body and ends with a semicolon after the method signature declaration. The IFoo.DefaultMethod() has a method body and doesn't require a terminating semicolon. This is how the compiler is able to syntactically differentiate an interface method that's abstract from an interface method with a default implementation. Moreover... IIRC, the default implementation method is actually used to create a virtual extension method that gets applied to concrete types not implementing the new interface method. This is used for backward support if an API author needs to add a method without breaking existing code.

+2

Shubham Yadav I would NOT say these were 'normal methods", but rather syntactic sugar that binds these default interface methods as extension methods on interface types without corresponding overrides methods. Also, these methods cannot refer to any instance properties or be inherited by the concrete class. IIRC... these default methods can only be accessed via a reference to the interface type. I'd have to spend some time experimenting with this in an IL disassembler to confirm. But... this is my current understanding. For additional information, check out the motivations for adding this controversial feature. https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-8.0/default-interface-methods#motivation The entire page for that link is worth reviewing. So is this one: https://github.com/dotnet/roslyn/blob/main/docs/features/DefaultInterfaceImplementation.md

+2

Shubham Yadav That looks to be about the size of it. However... professionally and personally speaking, we avoid the default interface methods and anything that isn't implicitly public abstract in interfaces. My development teams simply have not run into a situation where such a use case for default interface methods made sense to our development or maintenance work flows. As someone who has been professionally immersed with C# since its earliest alpha release back in 2000 or 2001, this is the first and only C# language design feature I can recall scratching my head about. So... I don't see me making use of this relatively new feature anytime soon, if ever. 😉

+2

Shubham Yadav I saw your DM... but I'm still having issues responding via DM. Feel free to post your followup question in an activity post or Q&A post and tag me so I'm notified. I'll try to answer when I can.

+1

David Carroll The reason why the default implementation is used is understandable . Here u mean that --> all methods in interface are abstract by default ( the compiler get that as per seeing that it has no method body and ends with semicolon ) but here we can also define the normal method ( which is having a method body or as in above senerio it is default implemented method ), like in abstract class it can have both abstract and normal methods ,, the interfaces can also have both abstract and normal methods !?! Ok now summing up all total - by default all methods are abstract but we can also define normal methods in interfaces ?

+1

David Carroll Thanks a lot for your help sir. As a sum up, from C#8.0 the interfaces have all methods abstract by default and if there is a need - it can also have static/private/protected members also which was not possible in earlier versions. Here's a article explaining this in simpler words- https://www.talkingdotnet.com/default-implementations-in-interfaces-in-c-sharp-8/