the reason to talk about varand theDynamic,is because you are usingEFFramework and useMVCthe time,They're going to be used very often.,so I had to mention them .,in order to contactEFand theMVCcan be well understood and skilled in the application.
just a little bit. var, Although it is called a mutable type , But he's not a type . , It 's just that he uses procedural inference to determine the type of .
We can write this:
static void Main (string[] args) { var i = 10;//Through program inference, I is finally inferred as int integer var j = "10";//Similarly, the program is inferred to be a character-type Console . WriteLine (i); Console.readkey (); }
Thereafter, I , J are identified, respectively, according to the int , string to deal with, no longer mutable, naturally become a strong type.
which cases do we use var to declare the variables?
In many cases,We don't know what type of variable to use,or I know what type I'm using now.,but it's very troublesome to write .,He's long .,or it's complicated ..It can be usedvarto declare,let the system infer the type of the variable itself.in useEFarchitecture of the time,It's typical, we'll use it frequently.var.
with var different, Dynamic is a purely mutable type, and is a type that, when the program is running, you can assign a variable of any type to him, and his type will change accordingly.
We can write this:
static void Main (string[] args) { //var i = 10;//Through program inference, I is eventually inferred as int integer //var j = "10";//Similarly, the program is inferred to be a character type //c Onsole. WriteLine (i); Console.readkey (); Dynamic i = ten; <span style= "White-space:pre" ></span> Console.WriteLine (i); i = "ten"; Console.WriteLine (i); i = DateTime.Now (); Console.WriteLine (i); }
will not have an error. At this point i, there is no definite type, you can modify it arbitrarily, and he will change, the typical weak type.
we may not often use this keyword to declare variables, but we are learning MVC , will be a lot of use, and later see the time to know, he declared the variable, at run time, will change. in General: For these two mutable types,var is not a type, but a type inference, which is done in the compile phase. Dynamic is a weak type that, at run time, determines the type of a variable based on the value of the variable.
. NET basic literacy--mutable type (var,dynamic)