0

What is the rationale behind explicit interface implementation? What problem/feature does it solve?

Or, put in other words, why .Net designer inserted explicit interface implementation in the language?

C# is a language that usually makes hard "shoot on your feet": as a simple example, it forces you to assing a variable before using it.

Explicit interface implementation goes on the opposite direction: it can make you easy to get an unexpected result from your code. Take this example:

interface IInterface
{
    int Compute(List<int> values);
}

class MyClass: IInterface
{
    public virtual int Compute(List<int> values)
    {
        //return count of list, null checks omitted
        return values.Count;
    }

    int IInterface.Compute(List<int> values)
    {
        // different implemention for Compute 

        //return count of even numbers within the list, null checks omitted
        return values.Where(x => x%2==0).Count();
    }

}

public void Test()
{
    List<int> values = new List<int>(new int[]{1, 2, 3, 4, 5});
    MyClass c = new MyClass();
    IInterface i = c; //no need for casting: MyClass implements IInterface

    Console.WriteLine("Are they the same object? {0}", object.ReferenceEquals(c, i)); // print true, obviously

    int res1 = c.Compute(values); 
    int res2 = i.Compute(values);
    Console.WriteLine($"res1 ={res1}, res2= {res2}"); // ops, different results calling the same method on the same object  
    // output is: res1 =5, res2= 2
}

I understand why it runs in that way: if the method Compute is called from an IInterface reference, it calls the explicit interface method. if it's called from a MyClass reference, it call the "regular" method.

And I also understand that I should implement on method delegating to the other (or not using interface implementation at all).

But why the language needs this strange construct? what if simply it did not exist in the language?

Gian Paolo
  • 4,161
  • 4
  • 16
  • 34
  • I guess the [MSIL spec](https://en.wikipedia.org/wiki/Common_Intermediate_Language) supports this, and C# as a language tries to expose all of IL's functionality – Andomar Mar 24 '18 at 16:38
  • Explicit interface implementation is necessary in some cases. e.g. `GetEnumerator()` has a different return value in `IEnumerable` and `IEnumerable` and since two functions with identical parameter can not have different return types, at least one function must be implemented as explicit. Second reason is duplication of Methods/properties. e.g. `byte[]` has `Length` property, but since it implements `ICollection` it must have also `Count`, that is duplicate to immutable value `Length`, where the `Count` from `ICollection` can change at runtime by adding or removing items from collection. – Julo Mar 24 '18 at 16:46
  • Simple answer would indeed be, to allow implementing "conflicting" interfaces. – Nyerguds Mar 24 '18 at 19:10
  • @Nyerguds, after reading the "duplicate" questions, I think the only reason to use explicit interface implementation is, as you say, to allow implementing conflicting interfaces (and to have 2 method with same name and parameters, but different return type, which is a similar issue). I don't agree with other reasons to use it: it's a tool that can lead to unexpected results in code, so use it only if you can't do without it. – Gian Paolo Mar 25 '18 at 09:04
  • Though I do agree it _is_ strange that, on top of these explicit interfaces, the class can have its _own_ function with the same signature as well. – Nyerguds Mar 25 '18 at 10:12
  • @Nyerguds, going back to .Net 1.0, in the pre generics era, you could implement `object ICloneable.Clone()` and `MyClass Clone()` in the same class, which could be very useful – Gian Paolo Mar 25 '18 at 10:48

0 Answers0