2

I was reading about implicit or explicit interface methods implementation, but I still not understand how it works and what are the benefits.

Having the code:

interface InterfaceOne
{
    void MethodOne();
}

class ClassOne : InterfaceOne
{
    public void MethodOne()
    {
        Console.WriteLine("hello from the class method");
    }

    void InterfaceOne.MethodOne()
    {
        Console.WriteLine("hello from the interface method");
    }
}

And the code from the main method:

    var c1 = new ClassOne();
    c1.MethodOne();

    InterfaceOne i1 = new ClassOne();
    i1.MethodOne();
    Console.ReadLine();

And here is the output:

hello from the class method

hello from the interface method

My questions:

  1. Why I don't have an error having a class with two methods with the same name and signature?

  2. When I'm using var keyword, how does the compiler choses which method to call?

  3. What are the benefits?

Buda Gavril
  • 21,409
  • 40
  • 127
  • 196

3 Answers3

1

Why I don't have an error having a class with two methods with the same name and signature?

Because that's how explicit interface implementation works. It allows you to have two versions of the same method. The method used is determined by what the type of reference used to invoke it was.

When I'm using var keyword, how does the compiler choses which method to call?

It is using type inference. The documentation states:

Local variables can be given an inferred "type" of var instead of an explicit type. The var keyword instructs the compiler to infer the type of the variable from the expression on the right side of the initialization statement

So its going to use whatever type the right hand side returned

What are the benefits?

Most of the time, none. But if you want your object to act differently when accessed through a certain interface, this is the way to do it.

BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
1

Why I don't have an error having a class with two methods with the same name and signature?

Because you're using a language feature which allows for that.

When I'm using var keyword, how does the compiler choses which method to call?

Using var doesn't matter here. All that matters is the type of variable on which method is called.

var c1 = new ClassOne();
InterfaceOne i1 = new ClassOne();

var c1Result = c1.MethodOne();
var c2Result = c2.MethodOne();

What are the benefits?

It's mostly meant to allow for implementing multiple interfaces with the same method:

interface One {
    int Foo();
}

interface Two {
    int Foo();
}

Without explicit interface implementation you wouldn't be able to have a class which implements both and provides different implementations for them.

MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
0

The benefit of Explicit is that sometimes you need it to resolve ambiguities. Imagine a scenario where you need to implement two different interfaces in the same class, which both have a method with the same name.

It's the sort of thing that's there more because sometimes it's necessary, not because it should be plan A of your design.

lgaud
  • 2,430
  • 20
  • 30