C#3.0(二)隐式类型化的本地变量

 

26.1 Implicitly typed local variables隐式类型化的本地变量

In an implicitly 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 specifies var as the type and no type named var is in scope, the declaration is an implicitly typed local variable declaration. For example:

在一个隐式类型化的本地变量和声明中,本地变量类型的声明过程是由使用的表达式初始化变量来推断的。当一个本地变量声明标示为var作为类型并且没有var类型名称在范围内,那么这个声明被视作隐式类型化的本地变量声明。例如:

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 above are precisely equivalent to the following explicitly typed declarations:

如上所述的隐式类型化的本地变量声明相当于下列显式的声明:

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:

一个本地变量声明在一个隐式类型化的本地变量声明中具有以下约束:

·         The declarator must include an initializer.

·         声明者必须包含一个构造者。

·         The initializer must be an expression. The initializer cannot be an object or collection initializer (§26.4) by itself, but it can be a new expression that includes an object or collection initializer.

·         这个构造者必须是一个表达式。这个构造者不能够是一个对象或者构造者集合($26.4)的自身,但是它可以是一个新的包含一个对象或者构造者集合的表达式。

·         The compile-time type of the initializer expression cannot be the null type.

·         在编译时刻构造者表达式的类型不能为null类型。

·         If the local variable declaration includes multiple declarators, the initializers must all have the same compile-time type.

·         如果本地变量声明包含多种声明者,那么构造者必须都具有相同的编译时刻类型。

The following are examples of incorrect implicitly typed local variable declarations:

以下是一些隐式类型化本地变量声明的错误例子:

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 specifies var as the type and a type named var is in scope, the declaration refers to that type; however, a warning is generated to call attention to the ambiguity. Since a type named var violates the established convention of starting type names with an upper case letter, this situation is unlikely to occur.

由于向后兼容性的原因,当一个本地变量声明标示为var作为类型并且在范围内已经有了一个var的类型,那么声明指向的是该类型;然而,编译器会产生该种混淆的警告。由于将类型声明为var违反了已经发布的关于将类型名称首字母大写的约定,所以这种情况不太可能发生。

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

for语句(§8.8.3)for-initializer以及using语句的resource-acquisition能够作为隐式类型化本地变量声明。同样的foreach语句的迭代变量也可以用来声明隐式类型化本地变量,这是为了迭代变量类型被表示为枚举的集合元素类型。在这个例子中

int[] numbers = { 1, 3, 5, 7, 9 };
foreach (var n in numbers) Console.WriteLine(n);

the type of n is inferred to be int, the element type of numbers.

类型n被表示为int,这是numbers的元素类型

你可能感兴趣的:(C#)