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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:21:30+00:00 2026-05-26T09:21:30+00:00

The following code demonstrates my question: public class DynamicExample { public void DoSomething() {

  • 0

The following code demonstrates my question:

public class DynamicExample
{
    public void DoSomething()
    {
        var x = new ExpandoObject();
        dynamic d = x;
        d.GetString = (Func<string>)(() => "Some Value");

        d.GetString().SomeStringExtension(); // Doesn't work - expected
        ((string)d.GetString()).SomeStringExtension(); // Works - expected
        Build(d).SomeStringExtension(); // Doesn't work - unexpected?
    }

    private static string Build(dynamic d)
    {
        return (string)d.GetString();
    }
}

public static class StringExtensions
{
    public static int SomeStringExtension(this string s)
    {
        return s.Length;
    }
}

The question is, why is there a difference for the compiler between casting the type inline to the extension method call and moving that cast out into a separate method?

  • 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-26T09:21:31+00:00Added an answer on May 26, 2026 at 9:21 am

    Build(d) is still a dynamic expression – the compile-time type of the method is dynamic even though you can see exactly what’s going on. That means extension methods won’t work.

    Basically the compiler follows reasonably simple rules to determine what the type of an expression is, and almost any expression involving dynamic ends up being considered as a dynamic expression. The exceptions to this are:

    • d is SomeType (always considered to be bool)
    • Casts, both direct and using as

    That’s it as far as I can remember, although I could be mistaken…

    Now the language could have been designed such that this case would statically resolve the call to Build as the only sensible one – after all, it’s impossible for d to be of any type which would change which method is called – but to specify the exact rules for that would make the language specification (and the compiler) significantly more complicated for relatively little gain.

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

Sidebar

Related Questions

The following code demonstrates a weird problem I have in a Turbo C++ Explorer
The following code demonstrates a misleading situation in which data is committed to the
This question is inspired by this question , which features the following code snippet.
The following code demonstrates my dilemma. The code creates a background thread which processes
A Java method call may be parameterized like in the following code: class Test
Consider the following code to demonstrate the question: let sequence = Seq.initInfinite (fun _
The following code demonstrates that the subclass named SubClass has a direct access to
Updated question to be more generic: I have the following code. When you swap
I've got the following classes: public class SupplierCategory : IEquatable<SupplierCategory> { public string Name
The following code demonstrates a problem with certain wx cursors on Windows (OSX cursors

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.