Before using the interface, never notice that the interface is divided into implicit implementation and display implementation. Yesterday when browsing the blog to see the relevant content, and now according to their own understanding of the record, convenient to meet the time to brush up.
In layman's terms, "display interface implementation" is the prefix of the method name using the interface name, whereas the traditional implementation is called "Implicit interface Implementation". Crap does not say, the example is as follows:
Interface IAnimal
{
void Dog ();
}
Class Animal:ianimal
{
public void Dog ()
{
Console.WriteLine ("Dog ...");
}
}
Defines a ianimal interface, which we would normally call:
Call through class
Animal Animal = new Animal ();
Animal. Dog ();
or through an interface call
IAnimal animal = new animal ();
Animal. Dog ();
Classes and interfaces can be called, in fact, this is the "implicit interface implementation."
So "Display interface implementation" is the god horse look?
Interface IAnimal
{
void Dog ();
}
Class Animal:ianimal
{
void Ianimal.dog ()
{
Console.WriteLine ("Dog ...");
}
}
Can only be called through an interface
IAnimal animal = new animal ();
Animal. Dog ();
Use the method of the class to implement the error, do not allow, "display interface implementation" only allow the implementation of the interface. If you really want to use a class to implement it, you have to do a forced type conversion.
Forced type conversion can be
Animal Animal = new Animal ();
(Animal as IAnimal). Dog ();
Since the display interface implementation so do not give force, why still exist, everything exists is reasonable. In a real project, sometimes a class inherits multiple interfaces, and the interfaces tend to have the same name, parameter, and type values. An explicit interface implementation can avoid unnecessary ambiguity (I don't see much in the project, probably because the project is too small).
Adaptive scenarios for display interface implementations and implicit interface implementations
- 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.
- When a class implements multiple interfaces, and the interface contains the same method signature, an explicit interface is used to implement it. Even if you do not have the same method signature, it is recommended that you use an explicit interface because you can identify which method belongs to which interface.
- Implicit interface implementations, both classes and interfaces, can access methods in an interface. An explicit interface implementation that can only be accessed through an interface.
Implicit and display implementations of C # interfaces