4

Assume I have a class C that inherits from 2 interfaces (I1 and I2). I also have two versions of a method (DoStuff), each taking one of the interfaces as a parameter. If I call DoStuff(C), which one gets called?

interface I1 { ... }
interface I2 { ... }
class C : I1, I2 { ... }

int DoStuff(I1 target) { ... }
int DoStuff(I2 target) { ... }

//What gets called here?
C target = new C()
DoStuff(target)

If I2 derives from I1, I think it's relatively simple - the I2 version gets called. I'm interested in the case where the interfaces are independent.

Assume I can't edit C, I1 or I2. And .NET 2.0 if that makes any difference.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Nigel Hawkins
  • 824
  • 1
  • 8
  • 23

3 Answers3

15

Neither gets called. If your two methods are overloads in the same class, the compiler does not attempt to disambiguate at all. It'll simply not compile your code, saying it's ambiguous between your two overloads as you declared target to be a type that implements both interfaces.

If you declare target to be of one of the interface types, or cast it at call-time (as Jon shows), then there's no ambiguity.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
6

As BoltClock says, it won't compile. However, it's easy to make it do what you want: just use an expression of the type that you want to use for the argument. For example:

C target = new C();
DoStuff((I1) target);

or

C target = new C();
I1 i1 = target;
DoStuff(i1);

Basically without any extra work, the overload resolution steps will find both methods in the candidate set, and determine that neither is "better" than the other, so overload resolution will fail.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
3

There will be an error when you try to compile it:

error CS0121: The call is ambiguous between the following methods or properties: 'DoStuff(I1)' and 'DoStuff(I2)'"

AakashM
  • 62,551
  • 17
  • 151
  • 186
Dima
  • 6,721
  • 4
  • 24
  • 43