C# Interface
Interface defines the syntax contract that all classes should follow when inheriting the interface. The interface defines the "what" part of the syntax contract, and the derived class defines the "how" part of the syntax contract.
Interface defines properties, methods and events, which are members of the interface. The interface only contains the declaration of members. Member definition is the responsibility of the derived class. Interfaces provide a standard structure that derived classes should follow.
Abstract classes are similar to interfaces to some extent, however, they are mostly used only when there are only a few methods declared by the base class and implemented by the derived class.
Declare interfaces
Interfaces are declared using the interface keyword, which is similar to the declaration of a class. Interface declarations are public by default. The following is an example of an interface declaration:
1 2 3 4 5 6 |
|
Example
The following example demonstrates the implementation of the above interface:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
|
When the above code is compiled and executed, it will produce the following results:
1 2 3 4 5 6 |
|
The above is [c# Tutorial】C# Interface (Interface) content, for more related content, please pay attention to the PHP Chinese website (www.php.cn)!