Thoughts Caused by the keyword VaR

Source: Internet
Author: User

In C #, the introduction of VaR is 3.0, but so far it has not been used in other places except in LINQ.

C # is a strongly typed language. var is introduced to support anonymous data types. variables declared by VAR are used inside the method and assigned values during initialization. After the first compilation by the compiler, the data type is determined. For the sake of code readability and later maintainability, it is generally not recommended to use anonymous data types when necessary. So what is necessary? The developer is not clear about the data type used, and the compiler can determine the data type based on the context. For example,

var students = from s in Students  
               where s.Name="allie"  
               select new { s.Sex, s.Age }; 

We only have the definition of student class, but not the definition of gender and age attributes, and we do not need to define such a class, therefore, we can use the keyword VaR to declare the students variable. The Compiler automatically generates a generic type (S. sex, S. the age parameter name. No more types will be generated if the number remains unchanged)

Without considering the readability of the Code, it is really convenient to use var. You can write less code.

This time, I will focus on VaR because my colleague encountered a problem and his code was probably like this,

Hashtable htable = new hashtable ();
Htable. Add (1, 2 );

Foreach (VAR item in htable)
{

// Item. Key = "XXX ";
}

Colleagues like to use var. His goal is to traverse the keys and values of each item in hashtable and then perform corresponding operations. However, the result is that there is no key or value attribute under the item (not visible in the smart prompt ). I don't know why, but I suggest you rename VaR as an explicit statement like dictionaryentry.

Here, when talking about something else, I usually use dictioary <key, value> instead of hashtalbe, and I feel that the performance will always be better than hashtable (I have never tested it, I just think it has applied the generic type, saving the box unboxing operation)

 

Related connections:

Http://stackoverflow.com/questions/902563/the-evilness-of-var-in-c

Http://www.cnblogs.com/jhh0111/archive/2008/10/23/1318223.html

Http://www.cnblogs.com/yuyijq/archive/2008/07/16/1244460.html

Http://www.qqread.com/csharp/p408564.html

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.