12
@{
    ViewBag.Username = "Charlie Brown";
    string title1 = string.Format("Welcome {0}", ViewBag.Username);
    var title2 = string.Format("Welcome {0}", ViewBag.Username);
}

In the MVC view I use the values like this:

@Html.ActionLink(title1, "Index")
@Html.ActionLink(title2, "Index")

Here, the title1 works fine. But the title2 ActionLink failed with a compiler error:

CS1973: 'System.Web.Mvc.HtmlHelper' has no applicable method named 'StandardHeader' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax.

string.Format() has quite a few overloads, but the return type is always string. Why does the variable declaration using var fail here?

1
  • 16
    I doubt that you got that error message from the code you've given. I suspect it's a different bit of code. Commented Feb 12, 2016 at 15:49

3 Answers 3

16

Okay, so we already know from comments and other answers that the problem is in dynamic. Since dynamic is bound on runtime, only at that time is the overload resolution and type validating is done.

So: if at least one of the parameters is dynamic, the overload resolution is done at runtime.

That is why this obvious mistake is allowed:

dynamic x = "";
int i = string.Format("{0}", x);

It doesn't bother if there isn't a string.Format overload that returns an int. It evaluates that later on.

Sign up to request clarification or add additional context in comments.

9 Comments

All expressions are evaluated at runtime, with the occasional exception of some operations on compile time literals. This is not specific to dynamic. What's deferred here is overload resolution and the the type of the expression is determined at runtime. That's different.
Okay, didn't know that. Thanks @Servy
Cool. That explains why. Thanks.
@Servy There is no overload resolution with int i = x; but it still compiles. Any thoughts on that? (From your comment I understood it shouldn't work, or did I read wrong?)
@PatrickHofman dynamic is implicitly convertible to any type.
|
11

The error message is telling you exactly what's wrong here:

Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax.

title2 is of type dynamic. You need to cast it to string, since you know that's what it is.

10 Comments

But string.Format() doesn't return dynamic.
@David - It will be dynamic if ViewBag.Username is dynamic. dynamic applies to the entire expression.
@CodeCaster Every method can return dynamic if any of its arguments is dynamic.
It would be worth explaining why the type of title2 is dynamic here. The explanation Lee gave is the crucial bit of this question, IMO.
@Servy: Nit pick: if the argument is dynamic. Making the parameter type dynamic has no effect.
|
7

It's the view bag which is dynamic. enter image description here

If you use actual username (instead of ViewBag.UserName)it would work. Or cast (string)ViewBag.Username into string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.