26.1 Implicitly typed local variables Implicitly typed local variables
InImplicitly typed local variable Declaration, The type of the local variable being declared is inferred from the expression used to initialize the variable. When a local variable declaration specifiesVaRAs the type and no type namedVaRIs in scope, the Declaration is an implicitly typed local variable declaration. For example:
In an implicitly typed local variable and description, the Declaration process of the local variable type is inferred by using the expression initialization variable. When a local variable declaration is markedVaRAs the type and noVaRIf the type name is within the range, this declaration is considered an implicit type local variable declaration. For example:
VaR I = 5;
VaR S = "hello ";
VaR d = 1.0;
VAR numbers = new int [] {1, 2, 3 };
VaR orders = new dictionary <int, order> ();
The implicitly typed local variable declarations abve are precisely equivalent to the following explicitly typed declarations:
The implicit local variable Declaration described above is equivalent to the following explicit declaration:
Int I = 5;
String S = "hello ";
Double D = 1.0;
Int [] numbers = new int [] {1, 2, 3 };
Dictionary <int, order> orders = new dictionary <int, order> ();
A local variable declarator in an implicitly typed local variable declaration is subject to the following restrictions:
A local variable Declaration has the following constraints in an implicitly typed local variable declaration:
·The declarator must include an initializer.
·The speaker must contain a constructor.
·The initializer must be an expression. the initializer cannot be an object or collection initializer (§26.4) By itself, but it can beNewExpression that matches des an object or collection initializer.
·The constructor must be an expression. This constructor cannot be an object or constructor set ($26.4But it can be a new expression that contains an object or constructor set.
·The compile-time type of the initializer expression cannot be the null type.
·The type of the constructor expression cannot beNullType.
·If the local variable Declaration has des multiple declarators, The initializers must all have the same compile-time type.
·If the local variable Declaration contains multiple annotators, the constructors must all have the same Compilation Time type.
The following are examples of incorrect implicitly typed local variable declarations:
The following are some Error Examples of implicit type local variable declaration:
VaR X;// Error, no initializer to infer type from
Var y = {1, 2, 3 }; // Error, collection initializer not permitted
VaR z = NULL;// Error, null type not permitted
For reasons of backward compatibility, when a local variable declaration specifiesVaRAs the type and a type namedVaRIs in scope, the Declaration refers to that type; however, a warning is generated to call attention to the ambiguity. Since a type namedVaRViolates the established convention of Starting Type names with an upper case letter, this situation is unlikely to occur.
When a local variable declaration is markedVaRAs the type and hasVaRType, the Declaration points to this type; however, the compiler will generate this obfuscation warning. Since the type is declaredVaRThis is not likely to happen because it violates the published conventions about capitalizing the first letter of a type name.
the for-initializer Of A for Statement (§ 8. 8.3) and the resource-acquisition Of A using Statement (§ 8. 13) can be an implicitly typed local variable declaration. likewise, the iteration variable of a foreach Statement (§ 8. 8.4) may be declared as an implicitly typed local variable, in which case the type of the iteration variable is inferred to be the element type of the collection being enumerated. in the example
ForStatement(§ 8. 8.3)OfFor-initializerAndUsingStatementResource-acquisitionCan actImplicitly typed local variable declaration. The sameForeachStatement iteration variables can also be used to declare implicitly typed local variables, so that the iteration variable type is represented as the enumerated set element type. In this example
Int [] numbers = {1, 3, 5, 7, 9 };
Foreach (var n in numbers) console. writeline (N );
The typeNIs inferred to beInt, The element typeNumbers.
TypeNIs representedInt, This isNumbersElement type