The usage of many keywords in C# is easy to confuse. Var and dynamic are one of the groups. They can both declare dynamically typed variables, but in essence they still have many differences. var has determined the type during the compilation stage. At initialization, the initialized value must be provided, while dynamic can not provide it, it determines the type at runtime.
Declaring a local variable is just a simplified syntax, which requires the compiler to infer specific data types based on an expression.
It can only be used to declare local variables inside a method, while dynamic can be used for local variables, fields, parameters.
3. Expressions cannot be transformed into var, but they can be transformed into dynamic.
4. The variable declared with var must be explicitly initialized, but there is no need to initialize the variable declared with dynam.
//var has determined the type during the compilation stage// var varError; var isIntType = ; (); //Dynamic does not perform any during compilation// type check, instead put type check in//Operation perioddynamic dyn = ; // dynamic dynOk; dyn = "hello world"; //Error string does not have a method [ method that does not exist in fn]//But it passes when syntax checking, no syntax errors are promptedvar s = A method that does not exist();//runtime error
Since dynamic only checks the type at runtime, there are errors sometimes, so it is necessary to use it. So when is it more convenient to use it? We first define a Person class, and then dynamically call the Talk method using reflection:
class Person { public void Talk(string msg) { (msg); } }
//dynamic can simplify code when reflecting t = typeof(Person); var obj = (t, null); ("Talk", , null, obj, new object[] { "hell world!" });
When reflecting, the method calls of traditional methods are often more cumbersome, while using dianmic is very simplified and intuitive:
dynamic obj = (t, null); ("hell world!");
Therefore, as long as dynamic is used properly, it is still a very good feature of C#.
Finally, let’s summarize the following:
People often compare the keyword var with dynamic. In fact, var and dynamic are completely different concepts and should not be put together for comparison. var is actually the "synthetic sugar" thrown to us during the compilation period. Once compiled, the compilation period will automatically match the actual type of the var variable and replace the declaration of the variable with the actual type. This looks like we declare it with the actual type when encoding. After dynamic is compiled, it is actually an object type. However, the compiler will specialize in the dynamic type, so that it does not perform any type checks during compilation, but puts the type checks at the runtime.
This can be seen from the editor window of visual studio. Variables declared in var support "intelligent perception" because the visual studio can infer the actual type of the var type, while variables declared in dynamic do not support "intelligent perception" because the compiler knows nothing about the type of its runtime. Using "intelligent perception" for dynamic variables will prompt "This operation will be parsed at runtime".