jugend
jugend

Reputation: 13

Implicit interface inheritance

I am just wondering which of this two methods, bool IsReal() from IFirstInterface or ISecondInterface, we are actually using in ExampleClass now:

interface IFirstInterface
{
    bool IsReal(); 
}
interface ISecondInterface
{
    bool IsReal();
}



public class ExampleClass : IFirstInterface, ISecondInterface
{
    public bool IsReal() {}
    //public bool IsReal is IFirstInterface or ISecondInterface method.
}

Please, could someone explain me how Implicit interface really works in C#.

Upvotes: 1

Views: 57

Answers (2)

Vadim Martynov
Vadim Martynov

Reputation: 8892

In the Explicit Interface Implementation guide you can read the following statement:

If a class implements two interfaces that contain a member with the same signature, then implementing that member on the class will cause both interfaces to use that member as their implementation.

You can implement one or both interfaces explicitly to override this behavior:

interface IFirstInterface
{
    bool IsReal(); 
}
interface ISecondInterface
{
    bool IsReal();
}

public class ExampleClass : IFirstInterface, ISecondInterface
{
    // will be used for IFirstInterface
    bool IFirstInterface.IsReal(){}

    // will be used for ISecondInterface
    public bool IsReal(){}
}

Upvotes: 2

Anil Goel
Anil Goel

Reputation: 271

For clarity purposes you should implement both functions public bool IFirstInterface.IsReal() {} public bool ISecondInterface.IsReal() {}

Upvotes: 0

Related Questions