C # explicit and implicit implementation of interfaces

Source: Internet
Author: User

Many people know the implementation of interfaces, but the implementation method of interfaces is dividedDisplay implementationAndImplicit implementationI don't know if many people know it! However, I think the company's technical department seldom mentioned this. I just want to write this blogs. Common Methods:

Public interface IReview
{
Void GetReviews ();
}
Public class ShopReview: IReview
{
Public void GetReviews (){}
}

This method isImplicit implementation:

IReview rv = new ShopReview ();

Rv. GetReviews ();

ShopReview rv = new ShopReview ();

Rv. GetReviews ();

You can call the GetReviews method.

 

Another way isDisplay implementation

Public interface IReview
{
Void GetReviews ();
}
Public class ShopReview: IReview
{
Void IReview. GetReviews (){}
}

This interface is implemented.

GetReviews can only be called through interfaces:

IReview rv = new ShopReview ();

Rv. GetReviews ();

 

The following method willCompilation Error:

ShopReview rv = new ShopReview ();

Rv. GetReviews ();


 Conclusion:Both the implicit implementation interface and class can access the display implementation. Only the interface can be accessed.

Show implementation benefits

1. Implementation of hidden code

2: In the system accessed through interfaces, callers can only access the system through interfaces instead of the underlying classes. PS: it can be applied to the company's current interface + Sevice architecture system. To avoid this, we always require you to access the service through interfaces. This is because interface access is not required now.

 

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.