Refresh
The default interface methods are a planned feature for C # 8 .
Original answer
There is no such exact function in C #, but extension methods solve the same problems as the default methods that were introduced to solve in Java.
To introduce LINQ-like functional methods to general collections in Java 8, language developers wanted to add methods to interfaces such as Iterable<> . In the end, .filter(a ->...).map(a ->...) much easier to read than map(filter(a ->...), a2 ->...) what they would have to do if they just added utility methods. However, simply adding a method signature to an interface would be a major change, because anyone who has ever implemented this interface suddenly has code that is not built in Java 8 if it had not implemented new methods. Therefore, they developed default implementation methods so that placing new methods in an existing interface does not violate existing code.
Several years ago, C # solved the same problem by introducing extension methods. Instead of actually changing the interface itself, the Extension method simply simplifies the use of the method (for example, the .Where() and .Select() methods for IEnumerable<> ) defined in the utility class, as if it were actually the target object
The restrictions placed on default extension and implementation methods make them very similar in scope. Each has its own advantages and disadvantages, which I will not go into here, but in essence these are just two different approaches to solving the same problem.
Since this relates to your specific question: one of the drawbacks of Extension methods is that (being static) they violate some of the best practices of object-oriented code: it is possible to have naming conflicts between them, and you cannot redefine them reliably, for example. Therefore, as a rule, it is best to avoid them unless you have a problem that is difficult to solve in any other way. If you're just hoping to provide a default implementation of the method, you'd better use a base class and expect people to extend your base class.
I think you will find that most Java experts would say the same thing about Java default implementations. They were not introduced before Java 8 because the prevailing view is that interfaces exist to determine what the object is capable of, and classes to determine how these things are executed. Of course, you can always find a few smart people who think that there is no good reason to have interfaces in the first place . But if you use interfaces, this is probably because you see value in defining a contract without providing implementation details. Default implementations were introduced to solve a very specific backward compatibility problem, and if you can avoid this problem in the first place, then I have no good reason to use them.
Extension methods are at the same time more dangerous and more powerful, so they can be found outside the backward compatibility problem, but they should be used sparingly and only when other more object-oriented approaches will not work.