Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement C# Interface member

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces how to implement C# interface members, the content is very detailed, interested friends can use for reference, I hope it can be helpful to you.

To implement a C # interface, a class can define an explicit interface member executor (Explicit interface member)

Implementations). The explicit C# interface member executor can be a definition of a method, a property, an event, or an index indicator, and the definition should be consistent with the discretionary name corresponding to the member.

ICloneable.Clone and IComparable.CompareTo in the above code are the explicit interface member executors.

Description:

You cannot access explicit interface member executors through full names in method calls, property access, and index indicator access. In fact, the explicit interface member executor can only be accessed through an instance of the interface, referring only to the member name of the interface.

Explicit C# interface member executors cannot use any access qualifiers and cannot add abstract, virtual, override or static modifiers.

Explicit C# interface member executors and other members have different access methods. Because it cannot be accessed by full name in method calls, property access, and index indicator access, the explicit interface member executor is private in a sense. However, they can be accessed through the instance of the interface, and they also have a certain public nature.

The explicit interface member executor is valid only if the interface name is written in the base class list when the class is defined, and the full-right name, type, and return type defined in the class are exactly the same as the explicit interface member executor, for example:

Using explicit C# interface member executors usually serves two purposes:

Because the explicit interface member executor cannot be accessed through an instance of the class, the implementation of the interface can be separated from the public interface. This explicit interface member executor can work if a class uses the interface only internally, and the class consumer does not use the interface directly.

The explicit interface member executor avoids confusion between interface members because of the same name. If a class wants to implement interface members with the same name and return type differently, it must use an explicit interface member executor. If there is no explicit interface member executor, the class cannot implement interface members with different names and return types.

The following definition is invalid because interface IComparable does not appear in the list of base classes when Shape is defined.

It is wrong to define ICloneable.Clone in Ellipse, because even though Ellipse implicitly implements the interface ICloneable,ICloneable, it still does not appear explicitly in the base class list defined by Ellipse.

The full name of the interface member must correspond to the member defined in the interface. As in the following example, the explicit interface member executor of Paint must be written as IControl.Paint.

The class that implements the interface can explicitly implement the members of the interface. When a member is explicitly implemented, the member cannot be accessed through an instance of the class, but only through an instance of the interface. Explicit interface implementations also allow programmers to inherit two interfaces that share the same member name and provide a separate implementation for each interface member.

The following example displays the size of the box in both metric and imperial units. The Box class inherits two interfaces, IEnglishDimensions and IMetricDimensions, which represent different systems of weights and measures. Both interfaces have the same member names Length and Width.

Output: Length (in): 30 ~ (th) width (in): 20 ~ ~ length (cm): 76.2 ~ ~ width (cm): 50.8

Code discussion: if you want the default measurement to be in imperial units, implement the Length and Width methods normally, and explicitly implement the Length and Width methods from the IMetricDimensions interface:

In this case, you can access imperial units from the class instance and metric units from the interface instance:

On how to implement the C# interface members to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report