Current location - Quotes Website - Signature design - What is the function of defining interfaces in C#
What is the function of defining interfaces in C#
C# interface definition C# does not support multiple inheritance, but there are many cases of multiple inheritance in the objective world. In order to avoid the complexity caused by traditional multiple inheritance, C# puts forward the concept of interface. Multiple inheritance functions can be realized through interfaces. The class or structure that implements the interface should be strictly consistent with the definition of the interface. An interface describes a set of related behaviors that can belong to any class or structure. Interfaces can be composed of methods, properties, events, indexers or any combination of these four member types. Interfaces cannot contain fields. Interface members must be male. Classes and structures can inherit from interfaces just like classes inherit from base classes or structures, and they can inherit from multiple interfaces. When a class or structure inherits an interface, it inherits the member definition but not the implementation. To implement an interface member, the corresponding member in the class must be public, non-static and have the same name and signature as the interface member. Properties and indexers of a class can define additional accessors for properties or indexers defined on an interface. For example, an interface can declare a property with a get accessor, while a class that implements the interface can declare the same property with both a get accessor and a set accessor. However, if the property or indexer is explicitly implemented, the accessors must match. Interfaces can inherit from other interfaces. A class can inherit an interface multiple times through the base class or interface it inherits. In this case, if the interface is declared as part of the new class, the class can only implement the interface once. If the inherited interface is not declared as part of the new class, it will actually be provided by the base class that declared it. The base class can use virtual members to implement interface members. In this case, the class that inherits the interface can change the interface behavior by overriding the virtual members. The declaration defining the interface adopts the following format: modifier interface name: inherited interface list

{

interface content;

} Among them, except interface and interface name, everything else is optional. Here is an example of C# interface definition with the following code: public interface isomeinterface {void update database (); } Interface Inheritance An interface can be inherited from one or more base interfaces. For example:

interface iothrerinterface: isomeinterface, ICOM {int compareto (object obj); } /s/29-11-26/1447115252.shtml