New Features of. NET Framework-implicitly typed Variables

Source: Internet
Author: User

With vs 2008, you can declare a variable so that the compiler can implicitly determine the type of the variable. This function is used to process the created variables. To use this function, you must useVarKeywords:

Var x = 5;

When this statement is used, the compiler uses 5 to determine the type of the variable. This statement is actually represented as follows:

Int x = 5;

The implicitly typed variable isStrong typeAfter the first compilation of the compiler, var will be replaced by a fixed type.

 

Implicitly typed variable keywords and var in js:

 

Javascript is a weak type language, and variables in javascript (including those declared using var) can be converted into types, as shown in the following javascript:

Var s = "abcd ";
S = 3;
Alert (s );

 

The above code assigned a string to s for the first time, and the second line of code assigned an integer. Such code has no problems in javascript. But in C #3.0, once the var variable is initialized, the type cannot be changed after it is determined. The following code cannot be compiled:

 

Var ss = "abcd ";
Ss = 44;

 

To sum up, using var to define variables has the following four features:

1. It must be initialized during definition. That is, it must be in the form of var s = "abcd" instead of the following:

 

Var s;
S = "abcd ";

 

It is inferred by the compiler according to the context, so all usage that cannot be inferred by the compiler is wrong. For example, var nullValue = null cannot be used like this. Because null is nothing, it is a null pointer and an uncertain thing. It cannot be used like this: var I = 5; I = "abc"; the compiler will infer from the first assignment that it is an integer, but then assign a value to it, what's going on?

2. var must be a local variable.

3. Using var to define variables is different from object. It is the same in efficiency as defining variables using a strong type. However, I suggest you declare variables in a strongly typed manner if you know the type of the variables in advance. Otherwise, a large amount of var will be used, making it difficult for developers to determine the type of a variable. This is not conducive to program maintenance and upgrade. (The implicit type of local variables is used only when the compiler can be inferred and the person cannot be inferred. It is not recommended to use them manually, explicit declaration of variable types can enhance the readability of the Code. This is a good programming habit. Do not use it unless C #3.0 provides such features .)

Although var has advantages and disadvantages, I personally think that if you convert a dynamic language into a C # language, you can consider using var to define variables. This is because the dynamic language does not have a type. to convert it to a strong C # language, you must specify a type for the variable, but it is very difficult to determine the type beforehand, it is better to specify it as var, and then the C # compiler determines the specific type of the variable. What should I do if I find that the dynamic language variable has changed the type during the conversion process? This problem can be solved using the "Anonymous class.

References: http://www.cnblogs.com/nokiaguy/archive/2008/06/10/1216970.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.