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?

1
  • @MarcinJuraszek it's not duplicate because the question you'me mentioned it's not answering my second question Commented Jan 14, 2017 at 0:49

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

can you give me the link where it states that " the compiler picks the most derived possible choice"?
@BudaGavril My bad, it appears that it just uses whatever is on the right. Added the link anyways
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.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.