C # syntax exercise (15): Interface

Source: Internet
Author: User
The interface is declared, unimplemented, and cannot be instantiated;
The interface can contain methods, attributes, events, and indexers, but has no fields;
All interface members are implicitly public. Do not use access modifiers;

Classes, structures, and interfaces can all inherit multiple interfaces;
The class that inherits the interface must implement the interface member, unless it is an abstract class;
Class implementation interface members must be public and non-static.

Example:
 
Using system; interface myinterface {int sqr (int x);} class myclass: myinterface {public int sqr (int x) {return x * X ;}} class program {static void main () {myclass OBJ = new myclass (); console. writeline (obj. sqr (3); // 9 myinterface INTF = new myclass (); console. writeline (INTF. sqr (3); console. readkey ();}}

  

An interface has different implementations:

Using system; interface myinterface {int method (int x, int y);} class myclass1: myinterface {public int method (int x, int y) {return X + Y ;}} class myclass2: myinterface {public int method (int x, int y) {return x-y;} class program {static void main () {myinterface intf1 = new myclass1 (); myinterface intf2 = new myclass2 (); console. writeline (intf1.method (3, 2); // 5 console. writeline (intf2.method (3, 2); // 1 Console. readkey ();}}

  

Display implementation interface (interface name. Method ):

Using system; interface myinterface1 {void method ();} interface myinterface2 {void method ();} class myclass: myinterface1, myinterface2 {/* indicates that access modifiers are not required for the Implementation interface; however, the display implementation method can only be accessed through the interface */void myinterface1.method () {console. writeline ("myinterfaceappsmethod");} void myinterface2.method () {console. writeline ("myinterface2_method") ;}} class program {static void main () {myinterface1 intf1 = new myclass (); myinterface2 intf2 = new myclass (); intf1.method (); // myinterfaceappsmethod intf2.method (); // myinterface2_method console. readkey ();}}

  

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.