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 ();}}