Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6375907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:40:35+00:00 2026-05-25T01:40:35+00:00

This may have been answered before. I see many dynamic method overload resolution questions,

  • 0

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
}
  1. Why, since the type isn’t statically known, does it not resolve to the overload accepting dynamic?
  2. Is it even possible for an overloaded method to use dynamic?
  3. What is the best way to resolve this?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T01:40:36+00:00Added an answer on May 25, 2026 at 1:40 am

    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:

    1. The argument to the method is a lambda with zero parameters. This gives us all three overloads as possibilities.
    2. 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.
    3. 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.
    4. 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:

    1. 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);
      
    2. Rename the dynamic overload to something like DynamicM. This way, you don’t have to deal with overload resolution.

    3. 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())
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This may have been answered before, but I cannot find a solution that works.
This question may have been asked before, but I had trouble finding an answer,
This has / may have been asked before, but, as far as I remember,
I have a question that may have been answered over 9000 times before but
This may have been asked before, but I could not find the answer. I
This may have been answered elsewhere but I could not find a suitable response.
I know this may have been answered but SubSonic 2.2 causes an error in
This may have been asked before, but it's really hard to search for terms
I know this question has been asked and answered before, but none of the
This may have already been answered in another post, but I just am not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.