Joke Collection Website - Mood Talk - Let’s talk about the explicit implementation and implicit implementation of interfaces and their respective uses

Let’s talk about the explicit implementation and implicit implementation of interfaces and their respective uses

The implicit implementation interface and class can be accessed. The explicit implementation only has the interface accessible.

Show implementation benefits 1: Implementation of hidden code 2: In a system that uses interface access, callers can only access through interface calls rather than underlying classes.

Display implementation interface description:

1. Explicit interface member execution bodies cannot be accessed through fully qualified names in method calls, attribute access and index indicator access. In fact, the explicit interface member executable can only be accessed through the instance of the interface, that is, only by referencing the member name of the interface;

2. The explicit interface member executable cannot use any access restrictions, and The abstract, virtual, override or static modifiers cannot be added, even if it is public;

3. Explicit interface member executors have different access methods from other members. Explicit interface member executables are private in the sense that they cannot be accessed through qualified names in method calls, property access, and index pointer access. But they can be accessed through instances of the interface, and they also have certain public properties;

4. Only when the class is defined, the interface name is written in the base class list, and the fully qualified name defined in the class When the type and return type are completely consistent with the explicit interface member executable, the explicit interface member executable is valid;

5. A class implements the interface both explicitly and implicitly. The explicit implementation method can only be directly accessed through the reference of the interface, and the implicit implementation method can only be directly accessed through the instance of the class;

6. If a class implements multiple interfaces, and there are many Two implemented interfaces contain interface declarations with the same signature. If one of them is explicitly implemented, then the methods with the same signature in the other interfaces must also be explicitly implemented.

Why do you need to display the implementation interface? There are the following benefits:

1. Because the explicit interface member execution body cannot be accessed through the instance of the class, it can be accessed from the public interface. The implementation part of the interface is separated separately. If a class only uses the interface internally, and users of the class will not use the interface directly, this explicit interface member executable can be useful.

2. Explicit interface member executors avoid confusion between interface members due to the same name. If a class wants to use different implementations of interface members with the same name and return type, it must use an explicit interface member executable. Without an explicit interface member executable, a class cannot implement interface members with different names and return types.