The main difference is the compilation time (for the object) and the execution time (for the dynamic) call forwarding. It is also called early and late binding. [Note: add a link to Microsoft.CSharp to compile the following code.]
object o = "Hello world";// fine because a derived type can be assigned to a base type dynamic d= "Hello world";// fine as well Type otype=o.GetType();// compiles because it confirms that object has a GetType() Type dtype=d.GetType();// also compiles but for another reason (ieno binding yet) string upperd= d.ToUpper(); // compiles because no binding yet ( anything goes :) string uppero= o.ToUpper(); // Fails to compile. Object has no ToUpper() method
If you comment on the last call, the application should work fine, because the CLR, when it reaches the second last call to d.ToUpper () at run time, will look for the ToUpper () method in the string type and find it there (because in the second statement d a string has been assigned). The last call did not compile, because during compilation in the System.Object style, a search for ToUpper () was performed, which, of course, would not be.
explorer
source share