Implicit and explicit implementations of C # interfaces

Source: Internet
Author: User

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

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.