. NET three ways to implement the interface

Source: Internet
Author: User

Excerpt from: http://www.cnblogs.com/zhangronghua/archive/2009/11/25/1610713.html

Generally speaking. NET provides three different implementations of interfaces, namely, implicit interface implementation, explicit interface implementation, and hybrid interface implementation. Each of these three ways has its own characteristics.

First of all, the implicit interface implementation, this is probably the most we use an interface implementation, because the stealth interface implementation is. NET is the default interface implementation method. Let's look at an example of an implicit interface implementation:

usingSystem;Internal classmyclass{ Public voidSomeMethod () {//declaring an Myinplement object in a way that uses an interfaceIMyInterface IOBJ =Newmyinplement (); Iobj.methoda ();//declaring a Myinplement object in the same way as a classMyinplement obj =Newmyinplement (); obj. MethodB ();}} Public classmyinplement:imyinterface{#regionIMyInterface Members/// <summary>///implementation of the method in the interface by means of an implicit interface implementation/// </summary> Public voidMethodA () {Console.WriteLine ("a");}/// <summary>///implementation of the method in the interface by means of an implicit interface implementation/// </summary> Public voidMethodB () {Console.WriteLine ("B");}#endregion} Public Interfaceimyinterface{voidMethodA ();voidMethodB ();}

The code above is simple, we declare an interface, and then we use the Myimplement class to implement the interface. When implementing the interface, we chose the implicit interface implementation, that is, the method signature defined in the interface is preceded by a modifier to define the signature of the implementation method, such as public Voidmethodb (). This implicit interface implementation is simple to use, but one drawback is that it allows the client to directly invoke the method in the implementation class (that is, by myinplementobj = Newmyinplement () This way of declaring an object, instead of invoking the method in the implementation class through an interface call (that is, by imyinterface iObj = new Myinplement (), declaring the object), this undoubtedly increases the coupling of the code and violates the principle of interface-oriented programming. So how do you prevent customers from calling implementation classes directly? Let's look at the explicit interface implementations below.

An explicit interface implementation is an implementation of a method in which a class implements an interface by using the style of the "interface name. Method Name" in the signature of the method, and not the modifier in the signature of the method, because all explicit interface implementations are private. NET to prohibit the direct invocation of the implementation class by the customer. Let's look at an example of an explicit interface implementation:

usingSystem;Internal classmyclass{ Public voidSomeMethod () {//declaring an Myinplement object in a way that uses an interfaceIMyInterface IOBJ =Newmyinplement (); Iobj.methoda ();//declaring a Myinplement object in the same way as a class will cause an error, because the MethodA method of Myimplement is private.Myinplement obj =Newmyinplement (); obj. MethodB ();}} Public classmyinplement:imyinterface{#regionIMyInterface Members/// <summary>///implementing methods in interfaces using explicit interface implementations/// </summary>voidImyinterface.methoda () {Console.WriteLine ("a");}/// <summary>///implementing methods in interfaces using explicit interface implementations///error, because modifiers are not allowed when an explicit interface is implemented/// </summary> Public  voidImyinterface.methodb () {Console.WriteLine ("B");}#endregion} Public Interfaceimyinterface{voidMethodA ();voidMethodB ();}

There are two errors in the code above because we violated the explicit interface implementation constraint where the error was made.

Finally, let's look at the hybrid interface implementation, the hybrid interface implementation is to use the implicit interface implementation and explicit interface implementation together, let's look at an example:

usingSystem;Internal classmyclass{ Public voidSomeMethod () {//declaring an Myinplement object in a way that uses an interfaceIMyInterface IOBJ =Newmyinplement (); Iobj.methoda ();//declaring a Myinplement object in the same way as a class does not cause an error, because the MethodA method of Myimplement is publicMyinplement obj =Newmyinplement (); obj. MethodB ();}} Public classmyinplement:imyinterface{#regionIMyInterface Members/// <summary>///implementing methods in interfaces using explicit interface implementations/// </summary>voidImyinterface.methoda () {Console.WriteLine ("a");}/// <summary>///implementation of the method in the interface by means of an implicit interface implementation/// </summary> Public  voidMethodB () {Console.WriteLine ("B");}#endregion} Public Interfaceimyinterface{voidMethodA ();voidMethodB ();}

In the above code we change the implementation of MEHTODB to an implicit interface implementation, so that the Myimplement.method modifier is public, and there is no error in the code.

The simple code example shows the difference between the three interface implementations, saying that before today I haven't noticed that implicit interface implementations can allow the implementation class to be called directly, whereas explicit interface implementations cannot be called directly because the methods in the implementation class are private. Looks like I'm right. NET understanding is very elementary ah, there are many basic knowledge has not mastered AH. I will try my best later.

----------->>>

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.