008. In C #, explicit interface vs implicit interface

Source: Internet
Author: User

Original Http://www.codeproject.com/Articles/1000374/Explicit-Interface-VS-Implicit-Interface-in-Csharp

(Aty said i) Introductionwhat is an explicit and an implicit interfaceWhen do I need an explicit interfacea more concise approach (ISP: interface isolation principle)Explicit Interfaces MoreConclusion IntroductionThe article discusses explicit interfaces, discusses their differences from implicit interfaces, and why it should be avoided. what is an explicit and an implicit interfaceThere are two ways to implement interfaces in C #: Explicitly and implicitly, the following interfaces are defined:   When we implement the interface implicitly, the code is as follows: The vast majority of developers implement interfaces using an implicit implementation. When we want to explicitly implement an interface, the Send method and the Recevie method are defined as private, and the code is as follows:simply put, the implicitly implemented method is public, and the explicitly implemented method is private. The question is why there are public and private points, and under what circumstances these two implementations are used. When do I need an explicit interfaceLet me first declare that you can use explicit implementations when your specific class does not need to include this interface abstraction.        I can feel how hard it is to understand the above statement. I will explain slowly, first of all we must understand the meaning of "abstraction".        Abstract: From the client's perspective, only the necessary methods and properties need to be displayed. Take WeatherService class as an exampleIf there are two layers to call it, the UI layer and the data layer. Two layers focus on the focus of the measurement is not the same. In the UI layer you only care about the properties of city and temperature, and the data layer only cares about the invocation of both the send and receive methods.For the best practice of programming, let me declare one more sentence:a good software architecture has many abstract classes. If an implicit implementation is used, the send and receive methods are public, and if called in the UI layer, the Send and receive methods are visible, but are not used. A good method is to explicitly implement the implementation, and the method is private, and the UI layer does not see both methods. How can you use these two methods? In the data layer, such as: a more concise approach (ISP: interface isolation principle)Interface Isolation principle: The client should not rely on the interface it does not need, and the dependency of one class on another should be based on the smallest interface. Using multiple specialized interfaces is better than using a single total interface.        (Baidu Encyclopedia).        A better implementation is to create a new interface, Iweather interface, as follows: all through the implicit implementation, so that in the process of invocation, the UI will define the variable type as iweather, the data layer is defined as irequest. (Note: In fact, aty to this still do not understand, define an abstract class better, interface definition behavior, perhaps the author just want to explain the concept of interface isolation) Explicit Interfaces more
When a class implements multiple interfaces, and the interface contains a method with the same name, an explicit implementation is required.
Explicit Interface Conclusion1. Explicit implementations should be avoided by using the ISP interface isolation Principle 2. In one case, you can use an explicit implementation when you cannot assign an interface behavior to a specific class for some reason

008. In C #, explicit interface vs implicit interface

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.