. NET basic literacy--mutable type (var,dynamic)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.