#1,053 – Implicit Conversion from Type dynamic
March 14, 2014 1 Comment
You can implicitly convert from an expression of type dynamic to any other type. What this means is that the compiler allows an implicit conversion (no cast operator) from the dynamic type to any other type and compilation succeeds.
While the conversion will always succeed at compile time, the actual assignment operation may fail at run-time, depending on whether a conversion between the actual type of the dynamic object and the target type succeeds. The dynamic keyword tells the compiler, “wait until run-time to figure out the type”.
dynamic dyn1 = "a string"; dynamic dyn2 = 42; // Everything below succeeds at compile-time string s = dyn1; int i = dyn1; // Fails at run-time (RuntimeBinderException) // (NOTE: Comment out above line to get // past exception). s = dyn2; // Fails at run-time (RuntimeBinderException) i = dyn2;