After the VS 2008 is installed, in the background code, the ReSharper plugin prompts suggestions for all local variables in the background, displaying the need for "use implicitly typed variable declaration", and using the code to modify the proposal to find " Use Var's hint, literally, will soon be known this is the new mechanism introduced in. NET framework3.5, which is the automatic type setting of local variables, depending on the type of object initialized on the right side of the equals sign. On the Internet to check the relevant explanations, recorded here:
One, the magic of Var
Added a variable declaring Var in c#3.0, which is similar to JavaScript's Var, but it's also different.
1. At the same point, he can declare any type of local variable with var.
2. At different points, he is only responsible for telling the compiler that the variable needs to infer the type of the variable based on the initialization expression and can only be a local variable.
second, the same point
He was able to declare:
Copy Code code as follows:
var integer = 10;
var name = "Edisundong";
var numbers = new int[] {1, 2, 3};
three, different points
var is just a keyword, he is not a new type in c#3.0, but is responsible for telling the compiler that the variable needs to infer the type of the variable based on the initialization expression, which is equivalent to:
Copy Code code as follows:
int integer = 10;
String name = "Edisundong";
int[] numbers = new int[] {1, 2, 3};
Four, note the point
1. The declaration must be assigned at the same time, because the declaration relies on the expression to the right of the assignment number if the following statement is present:
Copy Code code as follows:
var integer;
Integer = 10;
The implicitly typed locals must be initialized error is reported at compile time.
2. After using VAR to declare a local variable, he still has a strong type that can be tested as follows:
Copy Code code as follows:
var integer = 10;
Integer = "Edisundong";
The cannot implicitly convert type string to int error is reported at compile time.
3. The compile-time type of the initializer expression cannot be a null (NULL) type, and the compiler cannot infer the type of the local variable based on null, such as the following statement:
Copy Code code as follows:
Compile-time will report cannot assign <null> to a implicitly typed local error.
4. The initialization statement must be an expression, and the initialization expression cannot contain itself, but can be a new expression (that is, an anonymous type) that contains an object or collection initializer. If you can declare this:
Copy Code code as follows:
var coll = new Hashtable ();
5. Var's declaration is limited to local variables and can also be included in foreach, for, using statements. The following usage is incorrect:
Copy Code code as follows:
Class Program
{
private var i = 10; Global private variable.
static void Main (string[] args)
{ }
}
Compile-time will report the contextual keyword Var appear within a local variable declaration error.