The concept is very similar in C # and Java. The keywords for the interface are Interface, and an interface can extends one or more other interfaces. According to the practice, the name of the interface starts with uppercase letters "i". The following code is an example of a C # interface, which is exactly the same as the interface in Java:
Interface ishape {void Draw ();
If you derive from two or more interfaces, the name list of the parent interface is separated by commas, as shown in the following code:
Interface inewinterface: iParent1, iParent2 {}
However, unlike Java, the interface in the C # cannot contain a domain (Field). Also note that in C #, all methods in the interface are common methods by default. In Java, the method definition can carry a public modifier (even if it is not necessary), but in C #, the method explicitly interfaces to the interface specifies that the public modifier is illegal. For example, the following C # interface will generate a compilation error.
Interface ishape {public void Draw ();
The following example defines an interface called IControl, and includes a member method PAINT:
Interface icontrol {void paint ();
In the following example, interface IINTERFACE inherits from two base ports ibase1 and ibase2:
Interface IINTERFACE: IBASE1, IBASE2 {Void method1 (); void method2 ();
The interface can be implemented by the class. The identifier of the implemented interface appears in the base list of the class. E.g:
Class Class1: ifce1, IFACE2 {// Class member. }
When the class list is included, the base class is first appeared when the base class and the interface are included. E.g:
Class Classa: BaseClass, IFACE1, IFACE2 {// Class member. }
The following code segment defines the interface ifce, it only has one method:
Interface ifce {void showmyface ();
You cannot instantiate an object from this definition, but you can derive a class from it. Therefore, this class must implement the ShowmyFace abstract method:
Class CFace: ifce {public void showmyface () {Console.writeline ("Implementation");}}