Interfaces in C #

Source: Internet
Author: User
ArticleDirectory
    • 1. Public method implementation Interface Method
    • 2. Private methods cannot implement interface methods
    • 3. Implement special interface methods (1)
    • 4. Implement special interface methods (2)
    • 5. Conclusion

All the icons in this article are for personal understanding (refer to the metadata storage method in assembly), which may be different from the actual situation. Green indicates the public method, and red indicates the private method.

This article uses the following four cases to analyze how the interfaces in C # Work.

1. Public method implementation Interface Method

Although C # does not need to specify the access control method of the interface method when defining the interface, the default Interface methods are all public (this can be decompiled from the IlCode). The following is the Il code of the interface viewed by reflector.

 
. Class private interface abstract auto ANSI iControl {. MethodPublicHidebysig newslot abstract virtual instance void paint () cel managed {}}

The class that implements the interface must implement all interface methods. Generally, the interface implementation method is also public. Example:

Using system; interface iControl {void paint ();} public class editbox: iControl {PublicVoid paint () {console. writeline ("Pain method is called! ") ;}} Class test {static void main () {editbox = new editbox ();Editbox. Paint ();(IControl) editbox). Paint ();}}

ProgramThe execution result is:

 
Pain method is called! Pain method is called!

An interface is like a one-to-multiple table in a relational database. An interface corresponds to multiple interface methods, and each interface method corresponds to a public or private method in a virtual method table (VMT. The image of the above Code in the memory can be described:

We can see the call to the paint method and the call to the paint method through the interface. It can be seen that calling a method through an interface requires an extra conversion, so the execution efficiency is not as good as calling it directly.

2. Private methods cannot implement interface methods

If you want to directly implement an interface method as a private method, you cannot do it. In the following editbox code, the paint method has no special instructions. The default value is private, leading to Code failure:

Using system; interface iControl {void paint ();} public class editbox: iControl {Void paint (){Console. writeline ("Pain method is called! ");}Public void showpaint (){This. Paint ();(IControl) This). Paint ();} Class test {static void main () {editbox = new editbox (); editbox. showpaint ();}}

During compilation, the program displays the following compilation error: "editbox" does not implement "iControl. Paint ()" as the interface member ()". "Editbox. Paint ()" is static, non-public, or has an error return type ."

Why?

This is because the default access permission of the methods in the interface specification is public, and the default access permission in the class is default, that is, Private. Therefore, the permission range is reduced, and the permissions are not the same, therefore, you must change the class permission to public to execute the above Code.

3. Implement special interface methods (1)

The Code is as follows:

Using system; interface iControl {void paint ();} public class editbox: iControl { Void paint () {Console. writeline ("Pain method is called! ");} Void iControl. Paint () {Console. writeline ("iControl. Pain method is called! ");} Public void showpaint () {This. paint (); (iControl) This ). paint () ;}} class test {static void main () {editbox = new editbox (); editbox. showpaint (); // Editbox. Paint ();  (IControl) editbox). Paint (); }}

The editbox class has a private paint method, but this is not the implementation of the interface method (as analyzed in the previous example ). The editbox class also contains a "Void iControl. Paint ()" method, which is used to reproduce the painting method of the interface. This method is private (as can be seen through the Il code ).

Note: you cannot add any modifier, such as public or private, before "Void iControl. Paint ()", which is not allowed in the C # syntax. The IL code obtained by decompiling this method is as follows:

 
. Class public auto ANSI beforefieldinit editbox extends object implements iControl {...... MethodPrivateHidebysig newslot virtual final instance void iControl. Paint () cel managed {. Override iControl: Paint}}

Images in the memory can be simplified:

The program execution result is as follows:

 
Pain method is called! IControl. Pain method is called! IControl. Pain method is called!

We can use(IControl) editbox). Paint ()The method accesses the code because the interface method paint is public. But we cannot passEditbox. Paint ()The method accesses the code because the paint method of editbox is private. Inside the editbox, you can use the showpaint method to simultaneously access the private painting method and interface.IControl. PaintMethod.

4. Implement special interface methods (2)

If the pait method in editbox is public and the iControl. Paint method is provided at the same time, how does the program run? The Code is as follows:

Using system; interface iControl {void paint ();} public class editbox: iControl {Public void paint (){Console. writeline ("Pain method is called! ");}Void iControl. Paint (){Console. writeline ("iControl. Pain method is called! ") ;}} Class test {static void main () {editbox = new editbox ();Editbox. Paint ();(IControl) editbox). Paint ();}}

The program execution result is as follows:

Pain method is called! IControl. Pain method is called!

The memory layout during program execution is as follows:

It can be seen that the common paint method in editbox is not an interface implementation method, and the real interface implementation method is iControl. Paint, which causesEditbox. Paint ()Method and(IControl) editbox). Paint ()The execution results are not the same.

5. Conclusion

The implementation of interface methods is usually implemented through the public methods in the class;

In some special cases (Code hiding, two interfaces implemented by a class have the same interface methods), you need to implement a specific interface method.

Reprinted: http://www.cnblogs.com/zhenyulu/articles/377705.html

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.