If you use the second method then you HAVE to explicitly cast to the correct interface everytime. tt.DoStuff() alone will not work.
Main Topics
Browse All TopicsIf I inherit 2 or more interfaces that have the same method name/signiture what happens.
Do I get to implement it one, twice or will it not build; or something else.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
You need to implement both methods providing full qualified name:
interface IDimensions
{
float getLength();
float getWidth();
}
interface ISize
{
float getLength();
float getWidth();
}
class Box : IDimensions, ISize
{
float IDimensions.getLength()
{
...
}
float IDimensions.getWidth()
{
...
}
float ISize.getLength()
{
...
}
float ISize.getWidth()
{
...
}
}
From the client code, you can call these functions using interface reference.
<correction to previus post, sorry about that>
That explains most of it.
I assume using the first example, I could rewrite main as:
static void Main(string[] args)
{
ThisThat tt = new ThisThat();
IDoThis DoThis = tt;
IDoThat DoThat =tt
DoThis.DoStuff();
DoThat.DoStuff();
}
The extra bit im not so shour about is this example:
interface IDo
{
void DoStuff();
}
interface IDoThis : IDo
{
}
interface IDoThat : IDo
{
}
class ThisThat : IDoThis, IDoThat
{
void IDoThis.DoStuff()
{
Console.WriteLine("IDoThis
}
void IDoThat.DoStuff()
{
Console.WriteLine("IDoThat
}
}
static void Main(string[] args)
{
ThisThat tt = new ThisThat();
IDo Do = tt;
Do.DoStuff(); //which one is called
((IDo)tt).DoStuff(); //which one is called
}
Thanks in advance
Richard.
That code shouldn't compile.
DoStuff doesn't explicitly belong to the IDoThis and IDoThat interfaces, it belongs to the IDo interface. So in order to explicitly implement it you would have to use IDo.DoStuff().
Because DoStuff is the base for both IDoThis and IDoThat then what you end up with is an merger or the unique signatures, so there is only one DoStuff() method rather than on for each of the two derived interfaces. Therefore, no explicit declaration is needed, you simply use: void DoStuff(), to implement the method as defined in the base interface.
In practical terms you shouldn't realisticly inherit a class from two interfaces with the same base.
Hope that made sense.
Business Accounts
Answer for Membership
by: carl_tawnPosted on 2006-03-31 at 08:48:26ID: 16344085
You can implement one method that satisfies the signature for both interfaces.
doing stuff");
doing stuff");
class ThisThat : IDoThis, IDoThat
{
public void DoStuff()
{
Console.WriteLine("Doing stuff");
}
}
interface IDoThis
{
void DoStuff();
}
interface IDoThat
{
void DoStuff();
}
static void Main()
{
ThisThat tt = new ThisThat();
tt.DoStuff();
}
Or, you can specify one for each, in which case you have to cast to the interface you want to use:
class ThisThat : IDoThis, IDoThat
{
void IDoThis.DoStuff()
{
Console.WriteLine("IDoThis
}
void IDoThat.DoStuff()
{
Console.WriteLine("IDoThat
}
}
interface IDoThis
{
void DoStuff();
}
interface IDoThat
{
void DoStuff();
}
static void Main(string[] args)
{
ThisThat tt = new ThisThat();
((IDoThis)tt).DoStuff();
((IDoThat)tt).DoStuff();
Console.ReadLine();
}
Hope this helps.