. NET interface implementation methods

Source: Internet
Author: User

In general,. NET provides three different interface implementation methods: Implicit interface implementation, explicit interface implementation, and Hybrid Interface implementation. These three methods have their own characteristics.

First, let's look at the implementation of implicit interfaces. This is probably the most commonly used interface implementation, because the implementation of implicit interfaces is the default Implementation Method of. NET interfaces. Here is an example of implicit interface implementation:

Using System; internal class MyClass {public void SomeMethod () {// declare a Myinplement object IMyInterface iObj = new MyInplement (); iObj. methodA (); // declare a Myinplement object MyInplement obj = new MyInplement (); obj. methodB () ;}} public class MyInplement: IMyInterface {# region IMyInterface Members /// <summary> /// implement the method in the interface using the implicit interface /// </summary> public void MethodA () {Console. writeLine ("a") ;}/// <summary >/// use the implicit interface implementation method to implement the method in the interface /// </summary> public void MethodB () {Console. writeLine ("B") ;}# endregion} public interface IMyInterface {void MethodA (); void MethodB ();}

The code above is simple. We declare an interface and use the MyImplement class to implement the interface. When implementing the interface, we chose the implicit interface implementation, that is, adding a modifier before the method signature defined in the interface to define the signature of the implementation method, such as public voidMethodB (). This implicit interface is easy to implement, but has one drawback: it allows customers to directly call methods in the implementation class (that is, object Declaration through MyInplementobj = newMyInplement) instead of calling methods in the implementation class through interfaces (that is, declaring objects through IMyInterface iObj = new MyInplement ), this undoubtedly adds code coupling and violates the interface-oriented programming principles. So how can we prohibit customers from calling the implementation class directly? The following is an explicit interface implementation.

Explicit interface implementation refers to the implementation class using "Interface Name" in the method signature when implementing the interface method. the method name style is used to define the method name, And the modifier is not allowed in the method signature, because all methods implemented by explicit interfaces are private. NET to prohibit customers from calling the implementation class directly. The following is an example of the implementation of an explicit interface:

Using System; internal class MyClass {public void SomeMethod () {// declare a Myinplement object IMyInterface iObj = new MyInplement (); iObj. methodA (); // an error is returned when a Myinplement object is declared using the class method, because the MethodA method of MyImplement is private MyInplement obj = new MyInplement (); obj. methodB () ;}} public class MyInplement: IMyInterface {# region IMyInterface Members /// <summary> /// use the explicit interface implementation method to implement the method in the interface /// </summary> void IMyInterface. methodA () {Console. writeLine ("a") ;}/// <summary> /// the method in the interface is implemented using the explicit interface implementation method. // an error is reported, because explicit interface implementations do not allow modifiers /// </summary> public void IMyInterface. methodB () {Console. writeLine ("B") ;}# endregion} public interface IMyInterface {void MethodA (); void MethodB ();}

There are two errors in the above Code, because we violate the constraints of explicit interface implementation in the error.

Finally, let's look at the mixed interface implementation. The mixed interface implementation is to combine the implicit interface implementation with the explicit interface implementation. Let's look at an example below:

Using System; internal class MyClass {public void SomeMethod () {// declare a Myinplement object IMyInterface iObj = new MyInplement (); iObj. methodA (); // declare a Myinplement object using the class method. No error is reported because the MethodA method of MyImplement is public MyInplement obj = new MyInplement (); obj. methodB () ;}} public class MyInplement: IMyInterface {# region IMyInterface Members /// <summary> /// use the explicit interface implementation method to implement the method in the interface /// </summary> void IMyInterface. methodA () {Console. writeLine ("a") ;}/// <summary >/// use the implicit interface implementation method to implement the method in the interface /// </summary> public void MethodB () {Console. writeLine ("B") ;}# endregion} public interface IMyInterface {void MethodA (); void MethodB ();}

In the above Code, we changed the implementation of MehtodB to implicit interface implementation, so that the modifier of MyImplement. Method is public, and there is no error in the code.

Through simple code examples, we can see the differences between the three interface implementation methods. Before today, I have not noticed that the implicit interface implementation can allow the implementation class to be called directly, explicit interface implementation cannot be called directly because the methods in the implementation class are private. It seems that I have a very basic understanding of. NET, and I have not mastered many basic knowledge yet. Efforts will be made in the future.

 

Author: Zhang Ronghua
Source: http://zhangronghua.cnblogs.com/
The copyright of this article is shared by the author and the blog Park. You are welcome to repost this article. However, you must retain this statement without the author's consent and provide a clear link to the original article on the article page. Otherwise, you will be held legally liable.

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.