Explicitly implemented interface member

Source: Internet
Author: User

After reading the interface today, I was a little confused when I saw the implementation of explicit interface members. I searched for a good article on the Internet and wrote it down for later use!

Explicitly implemented interface member

Interfaces are protocols that support some features. The class that implements the interface must provide implementation details for the members specified in the interface. For example, the IEnumerator interface defines the member signature. Only when the member signature is implemented can a group of objects (such as a set) be enumerated. To implementIEnumeratorThe class must implement the Current, MoveNext, and Reset members.

When an interface member is explicitly implemented by a class, it can only be accessed by referencing the interface. This causes hidden interface members. Common Reasons for explicitly implementing interface members: not only to comply with the interface agreement, but also to improve it in some way (for example, provides an application to replace the strong type method of the weak type method of the interface ). Another common reason for explicitly implementing interface members is that they should not be called by developers. For example,GetObjectDataMembers are most explicitly implemented because they are called by the serialization infrastructure and are not used to call from code.

The following design guidelines help ensure that your library design is implemented only when needed.

If there is no good reason, you should avoid explicitly implementing interface members.

To understand explicit implementation, you must have high-level professional knowledge. For example, many developers do not know that explicit implementation members can be called by the public, even if their signatures are private. For this reason, explicitly implemented members are not displayed in the list of publicly visible members. Explicit Implementation of members also causes unnecessary binning of the value type.

If a member can only be called through an interface, explicitly implement the interface member.

This includes members that support the. NET Framework infrastructure (such as data binding or serialization. For example, the IsReadOnly attribute can only be accessed by the data binding infrastructure by referencing the ICollection interface. Because this rule is met, the List class explicitly implements this attribute.

Consider explicitly implementing interface members to simulate variants (that is, modifying the parameters or return types in the override members ).

This is usually done to provide a strongly typed version of interface members.

Consider explicitly implementing interface members to hide a member and add an equivalent Member with a better name.

In this way, the member can be effectively renamed. For example, Stream explicitly implements Dispose and provides the Close method at the corresponding position.

Do not use explicit members as security boundaries.

Explicitly implementing members does not provide any security. By referencing interfaces, these Members can all be called by the public.

If the explicitly implemented member function is intended to be specialized by the derived class, it is necessary to provide protected virtual members with the same function.

Explicit implementation members cannot be overwritten. If a new member is defined in a derived class, the derived class cannot call the base class implementation. You should use the same name as the explicit interface member or attach the Core to the interface member name to name the protected member.

 

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.