The var object type declaration introduced in.net framework

  • 2020-05-12 02:26:13
  • OfStack

Installed vs after 2008, in the code in the background, resharper plug-in to the Suggestions of the background all local variables, according to need to adopt "use implicitly typed local variable declaration", through the code changes, found that "use Var", through the literal meaning, can know this is soon. net framework3. 5 of the introduction of new mechanisms, namely to automatic type of local variables, the initialization type depends on the right hand side of the type of object. I have checked 1 relevant explanation on the Internet and recorded it here:
1. The amazing var
In C#3.0, a variable has been added to declare var, which is similar to, but different from, var for JavaScript.
1. Similarly, he can declare local variables of any type with var.
2. Different, he is only responsible for telling the compiler that the variable needs to infer the type of the variable from the initialization expression, and it can only be a local variable.
2. The similarities
He can state:
 
var integer = 10; 
var name = "edisundong"; 
var numbers = new int[] { 1, 2, 3 }; 

3. The differences between

var is just a keyword, it is not a new type in C#3.0, but it is responsible for telling the compiler that the variable needs to infer the type of the variable from the initialization expression, the above statement is equivalent to:
 
int integer = 10; 
string name = " edisundong "; 
int[] numbers = new int[] { 1, 2, 3 }; 

4. Pay attention to the point

1. The declaration must be accompanied by an assignment, because the declaration depends on the expression to the right of the assignment number, if the following statement exists:
 
var integer; 
integer = 10; 

Compile time reports Implicitly typed locals must be initialized error.

2. After declaring a local variable with var, he is still strongly typed and able to perform the following tests:
 
var integer = 10; 
integer = " edisundong "; 

Compile time will report Cannot implicitly convert type string to int error.

3. The compile-time type of the initializer expression cannot be null (null). The compiler cannot infer the type of the local variable based on null, as shown in the following statement:
 
var integer = null; 

Cannot assign will be reported at compile time < null > to an implicitly typed local error.

4. The initialization statement must be an expression, which cannot contain itself, but can be an new expression (that is, an anonymous type) containing an object or collection initializer. If you can state:
 
var coll = new Hashtable(); 

5. The declaration of var is limited to local variables and can also be included in foreach, for, using statements. The following usage is incorrect:
 
class Program 
{ 
 private var i = 10; // Global private variables.  
 static void Main(string[] args) 
 { } 
} 

Compile time will report The contextual keyword var may only within a local variable declaration error.

Related articles: