Method overload resolution using dynamic argument

c#, dynamic, overload-resolution, overloading

Solution

The problem here is type inference. The compiler is trying to find out which overload to use based on the argument, but it's also trying to find out what the type of the argument is based on the chosen overload. In the case of `M(() => DynamicObject())`, the process goes something like this:

- The argument to the method is a lambda with zero parameters. This gives us all three overloads as possibilities.

- The body of the lambda returns `dynamic`. Because there is an implicit conversion from `dynamic` to any other type, we now know all three overloads are good.

- Try choosing the best overload. In most cases, “best” means the most derived type. Because both `int` and `string` derive from `object`, the overloads with `int` and `string` are considered best.

- We now have two “best” overloads, which means the compiler can't actually choose one of them. The compilation fails.

Now, regarding possible solutions to your problem:

Make the type of the lambda explicit, either using cast or typed local variable:

M((Func<dynamic>)(() => DynamicObject()));

or

Func<dynamic> f = () => DynamicObject();
M(f);

Rename the dynamic overload to something like `DynamicM`. This way, you don't have to deal with overload resolution.

This one feels somewhat wrong to me: make sure the `dynamic` overload is the only one that fits, by casting to `object`:

M(() => (object)DynamicObject())

Problem

This may have been answered before. I see many "dynamic method overload resolution" questions, but none that deal specifically with passing a `dynamic` argument. In the following code, in `Test`, the last call to `M` cannot be resolved (it doesn't compile). The error is: the call is ambiguous between [the first two overloads of `M`]. ``` static void M(Func<int> f) { } static void M(Func<string> f) { } static void M(Func<dynamic> f) { } static dynamic DynamicObject() { return new object(); } static void Test() { M(() => 0); M(() => ""); M(() => DynamicObject()); //doesn't compile } ``` - Why, since the type isn't statically known, does it not resolve to the overload accepting `dynamic`? - Is it even possible for an overloaded method to use `dynamic`? - What is the best way to resolve this?

Original source