1. The interface must start with I;
2. Only declaration, not achieved;
3. The implementation of the class in the implementation method, the name must be consistent with the interface;
4. Implementation of the class in the implementation of the inheritance interface, the parent interface must be implemented in conjunction with the method;
5. Interface declaration, the default is public, this is different from class.
C#interface (Interface)
The interface defines the syntax contracts that should be followed when all classes inherit an interface. The interface defines the "what" part of the syntax contract, and the derived class defines the "How to" section of the syntax contract.
An interface defines properties, methods, and events, which are members of an interface. The interface contains only the declarations of the members. The definition of a member is the responsibility of the derived class. The interface provides the standard structure that a derived class should follow.
Interfaces make the class or structure that implements the interface consistent in form.
Abstract classes are somewhat similar to interfaces, but most of them are only used when only a few methods are declared by the base class by a derived class.
Defining Interfaces: MyInterface.cs
An interface uses the interface keyword declaration, which is similar to a class declaration. The interface declaration is public by default. The following is an instance of an interface declaration:
Interfaceimyinterface{voidmethodtoimplement();}
The above code defines the interface IMyInterface. Usually interface commands start with the letter I, this interface has only one method Methodtoimplement (), no parameters and return values, of course, we can install the requirements set parameters and return values.
It is important to note that this method does not have a specific implementation.
Next we will implement the above interface: InterfaceImplementer.cs
Using System;Interface IMyInterface{Interface Members void Methodtoimplement();}Class Interfaceimplementer : IMyInterface{ Static void Main() { Interfaceimplementer iImp = newinterfaceimplementer (); Iimp. Methodtoimplement} public void methodtoimplement () {< Span class= "PLN" > console. Writeline ( }} /span>
The Interfaceimplementer class implements the IMyInterface interface, and the implementation of the interface is similar to the class's inherited syntax format:
Classinterfaceimplementer:imyinterface
After inheriting the interface, we need to implement the method Methodtoimplement () of the interface, and the method name must match the method name defined by the interface.
Interface Inheritance: InterfaceInheritance.cs
The following example defines two interfaces IMyInterface and Iparentinterface.
If an interface inherits other interfaces, implementing a class or struct requires implementing the members of all interfaces.
The following instance IMyInterface inherits the Iparentinterface interface, so the interface implementation class must implement the Methodtoimplement () and Parentinterfacemethod () methods:
Using System;Interface Iparentinterface{ void Parentinterfacemethod();}Interface IMyInterface : Iparentinterface{ void Methodtoimplement();}Class Interfaceimplementer : IMyInterface{ Static void Main() { InterfaceimplementerIImp= New Interfaceimplementer();IImp.Methodtoimplement();IImp.Parentinterfacemethod(); } Public void methodtoimplement () { Span class= "Typ" >console. Writeline ( } public parentinterfacemethod () { console. Writeline ( }} /span>
The result of the instance output is:
Methodtoimplement() called. Parentinterfacemethod() called.
C # Basic Knowledge Review (10)---interface and interface inheritance