Interfaces in C #

Source: Internet
Author: User
Keywords nbsp by by realizing expressing seeing
Tags class code control default different expressing personal public

All of the illustrations in this article are purely personal (refer to the way metadata is stored in assembly) and may be different from the real situation. The green in the figure indicates the public method, and the red is the private method.

This article will use the following four cases to analyze how interfaces in C # work.

1, the public method to implement interface method

Although C # defines an interface without specifying the access control of an interface method, the default interface method is public (this can be seen from the decompile IL Code). The following is an interface Il code that is viewed using reflector

Class private interface abstract auto ANSI icontrol{. Method public hidebysig newslot abstract virtual instance void Paint () CIL Consolidator {}}

The class that implements the interface needs to implement all the interface methods. Typically, the implementation of an interface is also a public type. The following cases:

Using System; interface IControl {void Paint ();}   public class Editbox:icontrol {public void Paint () {Console.WriteLine (' Bon method is called! ');       }}class Test {static void Main () {EditBox editbox = new EditBox (); EditBox.      Paint (); ((icontrol) editbox).   Paint (); }}

The execution result of the program is:

Bon Method is called! Bon Method is called!

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

From the diagram we can see the direct call to the paint method and the call to the paint method via the interface. It is obvious that calling a method through an interface requires a lot of work to be done, so it's not as efficient as direct invocation.

Continue >> Next [1th] [page 2nd] [3rd] [4th]
Related Article

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.