Click here to Skip to main content
13,254,754 members (50,198 online)
Rate this:
Please Sign up or sign in to 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 4-Jul-12 8:07am
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.
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...
Vote 5 for the question -- pretty funny one.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You need explicit interface implementation. Here is the syntax:

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:
class MyClass {
    public int this[int index] { /* ... */ } 
    public double this[string index] { /* ... */ } //second one will not compile 
The solution is:
class MyClass {
    public int this[int index] { /* ... */ } 
    double MyInterface.this[string index] { /* ... */ } //should not have access modifiers 
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.


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

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.

Sandeep Mewara 4-Jul-12 14:54pm
5! for full detailed explanation.
Thank you, Sandeep.
Amol_Joshi 5-Jul-12 0:28am
great explanation!!!
Thank you, Amol.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.171114.1 | Last Updated 4 Jul 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100