1: When a class implements an interface, it is usually implemented using an implicit interface, which makes it easy to access interface methods and the methods and properties that the class itself has
2: When the class implements multiple interfaces and the interface contains the same method signature, the explicit interface is used at this time to implement. (Indicate which interface belongs to which method)
3: Implicit interface implementations: both classes and interfaces can access methods in the interface
4: Explicit interface implementation: can only be accessed through an interface.
Examples are as follows:
① Implicit
1 InterfaceAnimal2 3 {4 5 voidSpeak ();6 7 }8 9 classDog:animalTen { One Public voidSpeak () A { -Console.WriteLine ("Bark ..."); - } the } - //1. Invoking by Class -Dog dog =NewDog (); - Dog. Speak (); + //2. Call through the interface -Animal dog =NewDog (); +Dog. Speak ();
②-Explicit
1 InterfaceAnimal2 {3 voidSpeak ();4 }5 6 classDog:animal7 {8 voidAnimal.speak ()9 {TenConsole.WriteLine ("bark."); One } A } - - ① can only be called through an interface theAnimal dog =NewDog (); - Dog. Speak (); - ② using the class method call will error, you must use coercion type conversion -Dog dog =NewDog (); +(Dog asAnimal). Dog ();
Implicit and explicit implementations of C # interfaces