talk about the dynamic keyword
Objective
Today, the two-year-old girlfriend broke up.,at first I thought it was because of this fight.,because I haven't contacted her for a day today .,she's angry.,I said break up.,I mean, at first, I thought it was..and then I thought about it.,It's like it's going to be a bottle to sulfuric acid .,It's possible that the sulfuric acid in the bottle spilled out because of this drop .,There's a contradiction that's going to work.,Cherish the eyes of the people.
Body
That's a lot of crap , to be honest , c#4.0 provides a keyword for dynamic, which I said in the previous words . let 's see it today. Dynamic What the hell is it? . How does it work? ?
Let's look at a simple case :
static void Main (string[] args) { dynamic dyn = 1; Object obj = 1; The mouse is placed in "dyn" and "obj" at compile time to discover: //dyn: local variable dynamic (dyn) //obj: local variable object (obj) System.Console.WriteLine (dyn. GetType ()); System.Console.WriteLine (obj. GetType ()); }
Running this code will show the run-time types of dyn and obj :
System.Int32System.Int32
Add two lines after the WriteLine method:
dyn = dyn + 3; obj = obj + 3;
static void Main (string[] args) { dynamic dyn = 1; Object obj = 1; The mouse is placed in "dyn" and "obj" at compile time to discover: //dyn: local variable dynamic (dyn) //obj: local variable object (obj) System.Console.WriteLine (dyn. GetType ()); System.Console.WriteLine (obj. GetType ());d yn = dyn + 3; obj = obj + 3; }
You can see that for expression obj+=3 , the compiler reported an error , but does not report dyn+=3 Compilation .
The compiler does not check the expression for newspaper Dyn , because dyn is dynamic
Type conversions
Modify the Main code as follows :
Dynamic dyn = (dynamic) 1; int j = (int) dyn;
You can see that 1 is coerced into dynamic and then cast Int.
Dynamic can , however, implicitly convert any type and can also be converted back from other types .
So the above code is equivalent to the following :
Dynamic Dyn=1;int J=dyn;
Modify the Main code to add the following code :
This sentence will not be compiled, <span style= "Font-size:14px;color: #ff0000;" The ><strong>typeof operator cannot be used on dynamic types </strong></span> //console.writeline (typeof (Dynamic)); The following sentence is the right Console.WriteLine (typeof (List<dynamic>));
Analysis reason : If you are typeof (Dynamic) will report an error that the typeof operator cannot hold on a dynamic type , But if you write list<dynamic> Then the output is this :
System.Collections.Generic.List ' 1[system.object]
You can see that the output is : System.Object
Microsoft has an explanation for this :
Most of the cases,dynamic type is consistent with the behavior of type Object.but,does not use the compiler to includeDynamicthe operation of a type expression is parsed or type-checked.The compiler packs information about the operation,and this information is later used to calculate the run-time operation.in this process,typeDynamicthe variables are compiled into the typeObjectin the variable.so,typeDynamiconly exists at compile time,does not exist at run time.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Talk about the dynamic keyword