C # equivalent of Java 8 Methods by default in interface

I heard that in Java 8 there is the flexibility of defining functions in an interface. I think we can have some kind of default state with this function in all classes implementing such an interface.

So my question is: do we have such a function in C # today? Are there any Microsoft plans for this?

+9
source share
5 answers

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.

+5
source

The official language proposal for implementing the default interface in C # is described here:

https://github.com/dotnet/csharplang/blob/master/proposals/default-interface-methods.md

Currently, it is marked as a "proposal."

With that said, there are many use cases that are generally very strong. It is currently likely to be approved, as indicated by Mass Torgerson and Dustin Campbell in the video below. If so, it will almost certainly be released with C # 8.

https://channel9.msdn.com/Events/Build/2017/B8104

Around 53:00, the discussion begins and the demonstration is on display.

+3
source

C # 8.0 will introduce a new default interface implementation function. This is useful when adding new methods to ensure compatibility with existing interface implementations. It can also provide simple default implementations, as shown in the following example:

 public interface ILogger { void Log(LogLevel level, string message); void Log(LogLevel level, string format, params object[] arguments) { Log(level, string.Format(format, arguments)); } } 

New C # Features

+2
source

I agree with the statements made in the comments so far - C # does not support this. As far as I know, C # does not have a support plan, and it should not be supported. I do not agree with Java 8, including this feature; I think it combines interfaces and abstract base classes. As @AntP said in the comments, interfaces should be contracts (do not specify behavior).

Here are two possible projects that do the same thing (and sorry for hurryingly UML): enter image description here

Basically, you can either create an abstract base class that adds default implementations for all child classes, or you can have a base class for some child classes that implement interfaces, if that is what you are trying to do.

Obviously, other options are possible, this diagram is mainly for illustration only.

+1
source

C # does not support this, but I disagree with the comments, which say that interfaces should be non-contractual. Since the default implementations are not intended for implementation bags, this makes it difficult to keep clients free from the introduction of new updated interfaces and to prevent their old cold break if they do not use the new methods introduced in the new versions of the api that they used before.

The default methods in the java 8 implementation really helped solve the real problems that occurred after updating the api.

+1
source

Source: https://habr.com/ru/post/1261511/


All Articles