Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
see below c# code

public interface Itest1
{
     int Add();
}

public interface Itest2
{
    int Add();
}

public class test : Itest1,Itest2
{
    public int Add()
    {
        return 10 ;
    }
}


which Add method is implemented in test class

is it from Itest1 or Itest2 or both

many thanks...
Posted
Comments
Richard MacCutchan 4-Jul-12 13:18pm    
Neither, it's the one in class test. Before asking this question you could easily have tested it for yourself.
Sergey Alexandrovich Kryukov 4-Jul-12 15:55pm    
That is true, but I nevertheless answered and even up-voted the question.
I am not quite sure if OP deserves my vote, but I never saw such pathological case (and, frankly, could not even answer this question immediately); and trying to test pathological cases is an important skill; if you understand what do I mean...
--SA
Sergey Alexandrovich Kryukov 4-Jul-12 13:27pm    
Vote 5 for the question -- pretty funny one.
--SA

1 solution

You need explicit interface implementation. Here is the syntax:

C#
interface IFirstTest {int Add(); }

interface ISecondTest {int Add(); }

class DoubleTest : IFirstTest, ISecondTest {
    int IFirstTest.Add() { return ++value; } //pay attention: no access modifiers!
    int ISecondTest.Add() { value += 2; return value; }
    int value;
}


Even though your problem hardly can have practical sense, it illustrates the syntax which has benefits over implicit implementations and can solve real problems.

One of such problems is: how two write two indexed properties with the same number of indices of different types in one class? This will not compile:
C#
class MyClass {
    public int this[int index] { /* ... */ } 
    public double this[string index] { /* ... */ } //second one will not compile 
}
The solution is:
C#
class MyClass {
    public int this[int index] { /* ... */ } 
    double MyInterface.this[string index] { /* ... */ } //should not have access modifiers 
}

//where
interface MyInterface {
    double this[string index] { /* ... */ }
    // in the comment above, it should be "get;" or "get; set;" or "set;"
}


In general case, I would rather advise explicit implementation (even if there are no problems illustrated above); it helps to promote using classes purely through interfaces.

[EDIT]

To answer the original question, you should have tried to work with your class through both interfaces:
C#
Itest2 first = new test();
Itest1 second = new test();
first.Add();
second.Add();

You would see that in both cases, the same function is called; so, in this sense, you have implemented both interfaces at once. The case of implementing of two identical unrelated interfaces is pathological; I don't have it ever makes any practical sense, but you can see it's not prohibited by the compiler.

—SA
 
Share this answer
 
v3
Comments
Sandeep Mewara 4-Jul-12 14:54pm    
5! for full detailed explanation.
Sergey Alexandrovich Kryukov 4-Jul-12 15:22pm    
Thank you, Sandeep.
--SA
Rakesh S S 5-Jul-12 0:28am    
great explanation!!!
Sergey Alexandrovich Kryukov 5-Jul-12 0:38am    
Thank you, Amol.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900