1. Magic VaR
Added a variable declaration VaR in C #3.0, which is similar to the Javascript var, but also different.
1. similarities. It can be used to declare any type of local variables.
2. The difference is that it only tells the compiler that the variable needs to be inferred based on the initialization expression, and can only be a local variable.
Ii. Similarities
It can declare
VaR integer = 10;
VaR name = "edisundong ";
VAR numbers = new int [] {1, 2, 3 };
Iii. Differences
VaR is just a keyword. It is not a new type in C #3.0, but is responsible for telling the compiler that the variable needs to be inferred based on the initialization expression. The preceding statement is equivalent
Int integer = 10;
String name = "edisundong ";
Int [] numbers = new int [] {1, 2, 3 };
4. Notes
1. The value must be assigned at the same time during the declaration, because the Declaration depends on the expression on the right of the value assignment number, if the following statement exists:
VaR integer;
Integer = 10;
During compilation, the implicitly typed locals must be initialized error is reported.
2. After using VAR to declare a local variable, it still has a strong type and can be tested as follows:
VaR integer = 10;
Integer = "edisundong ";
The cannot implicitly convert type 'string' to 'int' error is reported during compilation.
3. The compile-time type of the initialization expression cannot be null. the compiler cannot infer the type of the out-of-box Variable Based on null. The following statement is provided:
VaR integer = NULL;
Cannot assign' will be reported during compilation' 'To an implicitly typed local error.
4. The initialization statement must be an expression. The initialization expression cannot contain itself, but it can be a new expression (namely, an anonymous type) that contains an object or a set initiator ).
Declare as follows:
VaR coll = new hashtable ();
5. The declaration of VaR is limited to local variables and can be included in foreach, for, and using statements. The following usage is incorrect:
Class Program
{
Private var I = 10; // global private variable.
Static void main (string [] ARGs)
{
}
}
The contextual keyword 'var' may only appear within a local variable declaration error is reported during compilation.