Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

implicit vs explicit interface implementation [duplicate]

Tags:

c#

.net

interface

People also ask

What is implicit interface and explicit interface?

With implicit interface implementations, the members of the interface are public in the class. With explicit implementations, in the class the interface members are not declared as public members and cannot be directly accessed using an instance of the class, but a cast to the interface allows accessing the members.

What is implicit and explicit interface implementation in C#?

An implicit interface implementation is where you have a method with the same signature of the interface. An explicit interface implementation is where you explicitly declare which interface the method belongs to.

What is explicit inheritance?

Explicit permissions are permissions that are set by default when the object is created, or by user action. Inherited permissions are permissions that are given to an object because it is a child of a parent object.

Can I declare any of the members of an interface Public explicitly why?

Interface is a contract and anywhere where you can access the interface, you should be able to access all the methods in it. In other words, all the methods declared in the interface are supposed to be public so it doesn't make sense stating it explicitly.


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.


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


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.