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 8893695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:18:37+00:00 2026-06-14T23:18:37+00:00

This question is somewhat an illustration to a related post , I believe the

  • 0

This question is somewhat an illustration to a related post, I believe the example below describes the essence of the problem.

class Program
{
    public static IList<string> GetData(string arg)
    {
        return new string[] {"a", "b", "c"};
    }

    static void Main(string[] args)
    {
        var arg1 = "abc";
        var res1 = GetData(arg1);
        Console.WriteLine(res1.Count());

        dynamic arg2 = "abc";
        var res2 = GetData(arg2);
        try
        {
            Console.WriteLine(res2.Count());
        }
        catch (RuntimeBinderException)
        {
            Console.WriteLine("Exception when accessing Count method");
        }

        IEnumerable<string> res3 = res2;
        Console.WriteLine(res3.Count());
    }
}

Isn’t it bad that the second call to GetData raises exception only because GetData received a parameter cast to dynamic? The method itself is fine with such argument: it treats it as a string and returns correct result. But result is then cast to dynamic again, and suddenly result data can not be treated according to its underlying type. Unless it’s explicitly cast back to a static type, as we see in the last lines of the example.

I failed to understand why it had to be implemented this way. It breaks interoperability between static and dynamic types. Once dynamic is used, it kind of infects the rest of the call chain potentially causing problems like this one.

UPDATE. Some people pointed out that Count() is an extension method, and it makes sense that it’s not recognized. Then I changed a call res2.Count() to res2.Count (from an extension method to a property of Ilist), but the program raised the same exception in the same place! Now that is strange.

UPDATE2. flq pointed to Eric Lippert’s blog posts on this topic, and I believe this post gives sufficient reasoning for why it is implemented this way:
http://blogs.msdn.com/b/ericlippert/archive/2012/10/22/a-method-group-of-one.aspx

  • 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-06-14T23:18:38+00:00Added an answer on June 14, 2026 at 11:18 pm

    The issue is that Count is an extension method.

    How would you locate extension methods at runtime? The information about which are ‘in scope’ is based on the ‘using’ statements in the particular file being compiled. These are not included in the compiled code at runtime, however. Should it look at all possible extension methods in all loaded assemblies? What about assemblies that were referenced by the project, but not yet loaded? There are a surprising number of boundary cases that crop up if you try to allow dynamic use of extension methods.

    The correct solution in this case is to call the static method in its non-extension form:

    Enumerable.Count(res2)

    Or, since you know it’s IList<T> in this case, just use the Count property:

    res2.Count <— EDIT: This doesn’t work because it’s an explicitly implemented interface property when implemented by the array.


    Looking at your question again, I see the real question is not about the extension method resolution per se, but rather why it can’t determine that there is a single method resolution possible and therefore know the type statically. I’ll have to ponder that a bit more, but I’m guessing it’s a similar question of boundary cases, particularly once you start considering multiple overloads.


    Here’s one nasty boundary case that could come up in the general case (though not directly applicable to your case, since you derive from Object).

    Suppose you have a class Base in assembly A. There is also a class Derived : Base in assembly B. In the Derived class, you have your code from above and you think there is only one possible resolution for GetData. However, now suppose that a new version of assembly A is published that has a protected GetData method with a different signature. Your derived class inherits this and the DLR dutifully allows for dynamic binding to this new method. Suddenly the return type may not be what you assumed. Note that all of this can occur without you recompiling assembly B. This means the pre-runtime compiler can’t assume that the DLR will resolve to the type the pre-runtime compiler believes is the only option, since the dynamic environment at runtime could yield a different type.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My problem is somewhat related to this question. I have a data as below
This question is related somewhat to the one i asked HERE . Now, i
This is somewhat related to the question posed in this question but I'm trying
Somewhat related to this question , but in the absence of any answer about
This question is somewhat related to Handling a timeout in EJB3 without using threads
This is somewhat related to this question , but not quite. I have two
This is somewhat related to this question : I have a table with a
This question is somewhat related to this one: VBA: What happens to Range objects
My question is somewhat related to this one: How does a generic constraint prevent
[This question is somewhat related to this question , but the answers are 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.