Implicit and display implementations of C # interfaces

Source: Internet
Author: User

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

      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 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.
      3. 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

Related Article

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.