Generic parameter inference and ambiguous function call - is there a workaround?

233 Views Asked by At

Possible Duplicate:
Ambiguous call between two C# extension generic methods one where T:class and other where T:struct

I've this two functions :

public static Degrees Convert<TInput>(this TInput input)
  where TInput : NumericValue, IDegreesBased, new()
{
  //Some stuff
}

public static SquarredMeters Convert<TInput>(this TInput input)
  where TInput : NumericValue, ISquarredMetersBased, new()
{
  // Some stuff
}

When I call new SquarredKilometers(10).Convert(), there is an error saying that the call is ambiguous between the two functions above. The SquarredKilometers class implements the ISquarredMetersBased interface.

EDIT : So it seems to be normal. Any workaround for this precise problem ? (Interface implementation)

3

There are 3 best solutions below

1
On

Your function signatures are identically - this can not work.
Try implementing your Interfaces explicit.

0
On

Constraints are not part of the method signature, so the methods have identical parameter types. Eric Lippert always explains C# the best: http://blogs.msdn.com/b/ericlippert/archive/2009/12/10/constraints-are-not-part-of-the-signature.aspx

0
On

You cannot have these two methods, because they accept same number of parameters. To make your code compile, you have to either change the signature of one of the methods or, you may implement the interface as suggested by Grumbler85.