Jako.Tako Jako.Tako - 1 year ago 102
C# Question

Weird dynamic type parameter issue

Simple code:

class Program
static void Main(string[] args)
dynamic income = "test";
var result = Test(income); // Why dynamic?
Test2(result); // WTF?

static string Test(string income)
return income;

static string Test2(int income)
return income.ToString();

The above code compiles with no errors and executes with an exception at runtime:
An unhandled exception of type Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' occurred in System.Core.dll

Can anyone explain why
is determined as

Answer Source

When you pass a dynamic value to an expression, that entire expression becomes dynamic.

The whole point of dynamic is to turn off all type-checking; the compiler will always assume dynamic, even if the code is obviously wrong.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download