Home » c# » implicit vs explicit interface implementation [duplicate]

implicit vs explicit interface implementation [duplicate]

Posted by: admin November 30, 2017 Leave a comment

Questions:

Possible Duplicate:
C#: Interfaces – Implicit and Explicit implementation

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

Answers:

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.

Questions:
Answers:

In layman’s terms, if a class inherits from 2 or more interfaces and if the interfaces happen to have the same method names, the class doesn’t know which interface method is being implemented if you use implicit interface implementation. This is one of the scenarios when you would explicitly implement an interface.

Implicit Interface Implementtation

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 an excellent video explaining this concept
Explicit Interface Implementation

Questions:
Answers:

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.