In C# you cannot have multiple implementation conflicts because (as stated in previous posts) it is impossible to inherit more than one class. But, when implementing multiple interfaces on a class, you can decide that a method is 'bound' to a specific interface.
This is an interesting concept which allows you to restrict a method so that it can only be used via a specific interface. This is known as
explicit interface implementation.
When implementing an interface on a class you have 2 options:
1. Implicit method declaration.
CODE
public void DoSomething()
2. Explicit method declaration.
CODE
void ISomeInterface.DoSomething()
I hope the following example will help explain the concept further...
CODE
using NUnit.Framework;
namespace DreamInCode.Tests.Answers
{
[TestFixture]
public class InheritanceConflictTests
{
[Test]
public void should_demonstrate_implicit_and_explicit_method_declarations()
{
InheritanceConflict viaClass = new InheritanceConflict();
IFoo viaIFoo = new InheritanceConflict();
IBar viaIBar = new InheritanceConflict();
Assert.AreEqual("A()", viaClass.A());
Assert.AreEqual("B()", viaClass.B());
// method C() is not visible via the class
Assert.AreEqual("A()", viaIFoo.A()); // no explicit implementation - will use class method
Assert.AreEqual("B()", viaIFoo.B()); // no explicit implementation - will use class method
Assert.AreEqual("IBar.B()", viaIBar.B()); // explicit implementation - will use explicit interface method
Assert.AreEqual("IBar.C()", viaIBar.C()); // explicit implementation - will use explicit interface method
}
}
public interface IFoo
{
string A();
string B();
}
public interface IBar
{
string B();
string C();
}
public class InheritanceConflict : IFoo, IBar
{
public string A() { return "A()"; }
public string B() { return "B()"; }
string IBar.B() { return "IBar.B()"; }
string IBar.C() { return "IBar.C()"; }
}
}