Ambiguous Overloads

  • Thread starter Thread starter John B
  • Start date Start date
J

John B

Hi all,
If I have the following methods:

public void foo(object o)
{
}

public void foo(string s)
{
}

public void foo(array a)
{
}

And I call:
foo(null);

I should (and do) get an ambiguous call error.

But if I comment out the foo(string) or foo(array)

then I do not get the error and it just goes to the not foo(object) one
(ie foo(array)).

Why is this?

JB
 
John B said:
If I have the following methods:

public void foo(object o)
{
}

public void foo(string s)
{
}

public void foo(array a)
{
}

And I call:
foo(null);

I should (and do) get an ambiguous call error.

But if I comment out the foo(string) or foo(array)

then I do not get the error and it just goes to the not foo(object) one
(ie foo(array)).

Why is this?

Because it picks the most specific one. Array and string aren't more or
less specific than each other, so there's ambiguity - but string is
unambiguously more specific than object (as is Array).
 
Back
Top