The realization of C # Interface's implicit and display and the practical technique of adapting scene _

Source: Internet
Author: User
Before using the interface, never noticed that the interface is divided into an implicit implementation and display implementation. Yesterday, when browsing the blog to see the relevant content, now according to their own understanding of the record, convenient in the future when the time to brush up.

Generally speaking, "Display interface implementation" is to use the interface name as the prefix of the method name, while the traditional implementation method called: "Implicit interface implementation." Crap does not say, examples are as follows:
Copy Code code as follows:

Interface IAnimal
{
void Dog ();
}
Class Animal:ianimal
{
public void Dog ()
{
Console.WriteLine ("Dog ...");
}
}
Defines a ianimal interface, which we typically call:
Call by class
Animal Animal = new Animal ();
Animal. Dog ();
or through an interface call
IAnimal animal = new animal ();
Animal. Dog ();

Both classes and interfaces can be invoked, in fact this is "implicit interface implementation".
So "Display interface implementation" Is God horse appearance?
Copy Code code as follows:

Interface IAnimal
{
void Dog ();
}
Class Animal:ianimal
{
void Ianimal.dog ()
{
Console.WriteLine ("Dog ...");
}
}
Can only be invoked through an interface
IAnimal animal = new animal ();
Animal. Dog ();
The method of class to implement the error will be, not allowed, "Display interface implementation" only allow interface implementation. If you really want to use a class to implement it, you have to do a cast.
After you force type conversion, you can
Animal Animal = new Animal ();
(Animal as IAnimal). Dog ();

Since the display interface implementation so not to force, why still exist, everything exists that is reasonable. In a real project, sometimes a class inherits more than one interface, and the interface tends to have values of the same name, parameter, and type. Explicit interface implementations can avoid unnecessary ambiguity (I don't see much in the project, probably because the project is too small).

display interface implementation and implicit interface implementation of the adaptive scene
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 implementation is used at this time. Explicit interfaces are recommended even without the same method signature, because you can identify which method belongs to which interface.

Implicit interface implementations, both classes and interfaces can access methods in the interface. Explicit interface implementations, accessible only through an interface.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.