implicit vs explicit interface implementation [duplicate]

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

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 them 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 Brad’s blog that there is boxing involved when using explicit implementation on value types so be aware of the performance cost.

Leave a Comment