C #3.0 highlights -- Keyword VaR and anonymous type

Source: Internet
Author: User

VaR is a special keyword used to initialize an anonymous object. If VaR and anonymous are combined, it is very personalized to initialize an anonymous object in C #3.0, this initialization method keeps me confused when I first came into contact with C #, for example, the following example:

 
VaR myfriend = new {name = "Tom", sex = "male "};

In this way, an anonymous object is initialized. Of course, the introduction of the keyword VaR is supported by a powerful compiler without explicit type declaration inProgramYou cannot allocate proper memory to this type of instance during running! For example, during compilation, the compiler will create a new anonymous type containing two string members (the first string member is name and the second string member is sex ).

Use the keyword VaR to declare a variable

There is not much to say about this:

VaR I = 10; // the same effect as int I = 10 var S = "Tom"; // the same effect as string S = "Tom //...

However, the following error occurs:

 
VaR I = 10; I = "Tom ";

During compilation, the compiler will perform "type deduction" and the compiler will recognize that I is an int variable, but the next lineCodeThe attempt to assign the string "Tom" to I will obviously cause compilation errors. in fact, Visual Studio is very intelligent. You don't have to wait until the compilation phase. When writing these two lines in IDE, an error is prompted.

Therefore, I learned var. Many people may like to use the VaR keyword when declaring any variable, but I will look back at the VaR keyword, after a few months, I looked back and found that the Code could not directly see the type of the variable, which deteriorated the maintainability of the Code. in <LINQ technical explanation> the author of p28 said this:

To obtain codes that can be maintained, do not use this keyword as needed because it is convenient to use the VaR keyword. try again when necessary, for example, when assigning an anonymous object to a variable.

In many cases, the variable type name is long (such as httpwebrequest), and httpwebrequest myrequest = (httpwebrequest) webrequest. create (XXX), followed by strong type conversion. In this case, I use VaR to declare many variables.

Related Article

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.