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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:04:11+00:00 2026-05-26T20:04:11+00:00

While refactoring some code, I came across this strange compile error: The constructor call

  • 0

While refactoring some code, I came across this strange compile error:

The constructor call needs to be dynamically dispatched, but cannot be because it is part of a constructor initializer. Consider casting the dynamic arguments.

It seems to occur when trying to call base methods/constructors that take dynamic arguments. For example:

class ClassA
{
    public ClassA(dynamic test)
    {
        Console.WriteLine("ClassA");
    }
}

class ClassB : ClassA
{
    public ClassB(dynamic test)
        : base(test)
    {
        Console.WriteLine("ClassB");
    }
}

It works if I cast the argument to object, like this:

public ClassB(dynamic test)
    : base((object)test)

So, I’m a little confused. Why do I have to put this nasty cast in – why can’t the compiler figure out what I mean?

  • 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-26T20:04:12+00:00Added an answer on May 26, 2026 at 8:04 pm

    The constructor chain has to be determined for certain at compile-time – the compiler has to pick an overload so that it can create valid IL. Whereas normally overload resolution (e.g. for method calls) can be deferred until execution time, that doesn’t work for chained constructor calls.

    EDIT: In “normal” C# code (before C# 4, basically), all overload resolution is performed at compile-time. However, when a member invocation involves a dynamic value, that is resolved at execution time. For example consider this:

    using System;
    
    class Program
    {
        static void Foo(int x)
        {
            Console.WriteLine("int!");
        }
    
        static void Foo(string x)
        {
            Console.WriteLine("string!");
        }
    
        static void Main(string[] args)  
        {
            dynamic d = 10;
            Foo(d);
        }
    }
    

    The compiler doesn’t emit a direct call to Foo here – it can’t, because in the call Foo(d) it doesn’t know which overload it would resolve to. Instead it emits code which does a sort of “just in time” mini-compilation to resolve the overload with the actual type of the value of d at execution time.

    Now that doesn’t work for constructor chaining, as valid IL has to contain a call to a specific base class constructor. (I don’t know whether the dynamic version can’t even be expressed in IL, or whether it can, but the result would be unverifiable.)

    You could argue that the C# compiler should be able to tell that there’s only actually one visible constructor which can be called, and that constructor will always be available… but once you start down that road, you end up with a language which is very complicated to specify. The C# designers usually take the position of having simpler rules which occasionally aren’t as powerful as you’d like them to be.

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

Sidebar

Related Questions

While refactoring some old code I have stripped out a number of public methods
I unfortunately was doing a little code archeology today (while refactoring out some old
I came across a few articles like this one , which suggest that some
While doing some refactoring I've found that I'm quite often using a pair or
While refactoring code and ridding myself of all those #defines that we're now taught
During a code review with a Microsoft employee we came across a large section
Recently I have been refactoring some of my C# code and I found a
I'm refactoring some code and I have written a method that modifies a Dictionary
I was refactoring some old code of a simple script file parser when I
I'm refactoring some code. Right now there are quite a few places with functions

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.