tags:

views:

1892

answers:

2

Would someone explain the differences between these two beasts and how to use them. AFAIK, many pre.2.0 classes were implemented without generic types, thus causing latter version to implement both flavors of interfaces. Is the the only case why one would need to use them?

Can you also explain in depth how to use them.?

Thanks

+17  A: 

There is a good and pretty detailed blog post here:

http://blogs.msdn.com/mhop/archive/2006/12/12/implicit-and-explicit-interface-implementations.aspx

Basically with implicit interface implementation you access the interface methods and properties as if they were part of the class. With explicit interface implementations you can only access then when treating it as that interface.

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

The below rules are from Brad Abrams design guidelines blog.

  • Do not use explicit members as a security boundary. They can be called by any client who cast an instance to the interface.
  • Do use explicit members to hide implementation details
  • Do use explicit members to approximate private interface implementations.
  • Do expose an alternative way to access any explicitly implemented members that subclasses are allowed to override. Use the same method name unless a conflict would arise.

It's also mentioned in the comments in the Brad's blog that there is boxing involved when using explicit implementation on value types so be aware of the performance cost.

Andrew Barrett
looks good thanks.. also interested in implementation details...
Added some more detail for you.
Andrew Barrett
Thanks... I'm accepting your answer!
You may want to consider the performance issues around explicit interface implementations as pointed out in the comments section of this blog post: http://blogs.msdn.com/brada/archive/2003/11/15/50721.aspx
Eric Smith
Cheers man, edited based on that info.
Andrew Barrett
A: 

There is one more way to look at it, from the labyrinthine implementation itself, here: http://blogs.msdn.com/cbrumme/archive/2003/05/03/51381.aspx.

But in short, implicit implementation gives you an is-a type conversion, explicit implementation won't be accessible unless the object is explicitly type cast to that interface type.

Tanveer Badar