Implicit implementation with explicit interface

Possible duplicate:
C #: interfaces - implicit and explicit implementation

Someone explain the differences between the two beasts and how to use them. AFAIK, many pre.2.0 classes were implemented without generic types, which forced the latest version to implement both interface options. The only case why you had to use them?

Can you also explain in detail how to use them?

thank

+58
c # interface
Feb 28 '09 at 21:29
source share
3 answers

There is a good and fairly detailed blog post about this.

Basically, with an implicit interface implementation, you get access to the methods and properties of the interface as if they were part of a class. With explicit implementations of an interface, you can only access them when considering this interface.

In terms of when you will use one over the other, sometimes you need to use an explicit implementation of an interface, because you either have a property / method with the same signature as the interface, or you want to implement two interfaces with the same signatures and different implementations for those properties / methods that match.

The following are the rules from Brad Abrams Design Guidelines Blog .

  • Do not use explicit elements as a safety margin. They can be called by any client that passes the instance to the interface.
  • Use explicit members to hide implementation details
  • Use explicit members to approximate the implementation of a private interface.
  • Provide an alternative way to access any explicitly implemented elements that subclasses can override. Use the same method name if there is no conflict.

He also mentioned in the comments on Brad's blog that there is boxing when using the explicit implementation of value types, so keep in mind the cost of performance.

+92
Feb 28 '09 at 21:34
source share

In layman's terms, if a class inherits from 2 or more interfaces, and if the interfaces have the same method names, the class does not know which interface method is implemented if you use an implicit interface implementation. This is one of the scenarios when you explicitly implement the interface.

Implicit Interface Implementation

public class MyClass : InterfaceOne, InterfaceTwo { public void InterfaceMethod() { Console.WriteLine("Which interface method is this?"); } } interface InterfaceOne { void InterfaceMethod(); } interface InterfaceTwo { void InterfaceMethod(); } 

Explicit interface implementation

 public class MyClass : InterfaceOne, InterfaceTwo { void InterfaceOne.InterfaceMethod() { Console.WriteLine("Which interface method is this?"); } void InterfaceTwo.InterfaceMethod() { Console.WriteLine("Which interface method is this?"); } } interface InterfaceOne { void InterfaceMethod(); } interface InterfaceTwo { void InterfaceMethod(); } 

The following link has a great video explaining this concept
Explicit interface implementation

+16
Sep 21 '12 at 12:35
source share

There is another way to look at this from the most labyrinth implementation: http://blogs.msdn.com/cbrumme/archive/2003/05/03/51381.aspx .

But, in short, an implicit implementation gives you a type-type conversion, an explicit implementation will not be available unless the object is explicitly applied to the interface type.

+3
Mar 01 '09 at 9:37
source share



All Articles