1、26.2 Extension methods 扩展方法. 626.2.1 Declaring extension methods 声明扩展方法. 626.2.2 Importing extension methods 导入扩展方法. 726.2.3 Extension method invocations 扩展方法的调用. 726.3 Lambda expressionsLambda表达式. 826.3.1 Lambda expression conversionsLambda 表达式转换. 1026.3.2 Type inference类型推导. 1126.3.3 Overload reso
2、lution重载决议. 1326.4 Object and collection initializers对象和集合的初始化器. 1426.4.1 Object initializers对象初始化器. 1426.4.2 Collection initializers集合初始化器. 1626.5 Anonymous types 匿名类型. 1726.6 Implicitly typed arrays隐型数组(Implicitly typed arrays). 1826.7 Query expressions 查询表达式. 1926.7.1 Query expression translation
3、 查询表达式的转换. 2026.7.1.1 where clauses where子句. 2126.7.1.2 select clauses select 子句. 2126.7.1.3 group clauses group子句. 2126.7.1.4 orderby clauses orderby 子句. 2226.7.1.5 Multiple generators 多重产生器(generator) . 2226.7.1.6 into clauses info 子句. 2326.7.2 The query expression pattern 查询表达式模式. 2326.7.3 Formal
4、 translation rules 正式的转换规则. 2426.8 Expression trees 表达式树. 2626. Overview of C# 3.0 (C# 3.0概述)C# 3.0 (“C# Orcas”) introduces several language extensions that build on C# 2.0 to support the creation and use of higher order, functional style class libraries. The extensions enable construction of compos
5、itional APIs that have equal expressive power of query languages in domains such as relational databases and XML. The extensions include:C# 3.0 (“C# 魔兽(Orcas)”) 引入了几个构建在C# 2.0上的语言扩展,用来支持创建和使用更高级的函数式(functional或译:泛函)类库。这些扩展允许组合(compositional)APIs的构造,这些APIs与关系数据库和XML等领域中的查询语言具有同等的表达力。 Implicitly typed
6、 local variables, which permit the type of local variables to be inferred from the expressions used to initialize them. 隐型局部变量,允许局部变量的类型从初始化它们的表达式推导而来。 Extension methods, which make it possible to extend existing types and constructed types with additional methods. 扩展方法,使得使用附加(additional)的方法扩展已存在的类型
7、和构造类型成为可能。 Lambda expressions, an evolution of anonymous methods that provides improved type inference and conversions to both delegate types and expression trees. Lambda 表达式,是匿名方法的演进,可提供改良的类型推导和到dalegate类型和表达式树的转换。 Object initializers, which ease construction and initialization of objects. 对象初始化器,简
8、化了对象的构造和初始化。 Anonymous types, which are tuple types automatically inferred and created from object initializers. 匿名类型,是从对象初始化器自动推导和创建的元组(tuple)类型。 Implicitly typed arrays, a form of array creation and initialization that infers the element type of the array from an array initializer. 隐型数组,数组创建和初始化的形
9、式,它从数组初始化器推导出数组的元素类型。 Query expressions, which provide a language integrated syntax for queries that is similar to relational and hierarchical query languages such as SQL and XQuery. 查询表达式,为类似于关系型和层次化查询语言(比如SQL和XQuery)提供一个语言集成(intergrated)的语法。 Expression trees, which permit lambda expressions to be
10、represented as data (expression trees) instead of as code (delegates). 表达式树,允许lambda表达式表示为数据(表达式树)而不是代码(delegate)。This document is a technical overview of those features. The document makes reference to the C# Language Specification 1.2 (1 through 18) and the C# Language Specification 2.0 (19 throug
11、h 25), both of which are available on the C# Language Home Page (本文档是这些特征的技术概述。文档引用了C#语言规范1.2(1-18)和C#语言规范2.0(19-25),这两个规范都在C#语言主页上(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 express
12、ion 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作为类型,并且该范围域(scope)中没有var名称的类型存在,这个声明就
13、称为隐型局部声明。例如:var i = 5;var s = Hello;var d = 1.0;var numbers = new int 1, 2, 3;var orders = new Dictionary();The implicitly typed local variable declarations above are precisely equivalent to the following explicitly typed declarations:上面的隐型局部变量声明精确地等同于下面的显型(explicitly typed)声明:int i = 5;string s = d
14、ouble d = 1.0;int numbers = new int 1, 2, 3;Dictionary orders = new DictionaryA local variable declarator in an implicitly typed local variable declaration is subject to the following restrictions:隐型局部变量声明中的局部变量声明符(declarator)遵从下面这些约束: The declarator must include an initializer. 声明符必须包含初始化器。 The ini
15、tializer 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),但是它可以是包含一个对象或集合初始化器的一个new表达式。 The compile-time type of the initi
16、alizer 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 variabl
17、e declarations:下面是一些不正确的隐型局部变量声明的例子:var x; / Error, no initializer to infer type fromvar y = 1, 2, 3;/ Error, collection initializer not permittedvar z = null; / Error, null type not permittedFor reasons of backward compatibility, when a local variable declaration specifies var as the type and a typ
18、e 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作为类
19、型,而范围域中又存在叫var的类型,则这个声明会推导为那个叫var的类型;然后,会产生一个关注含糊性(ambiguity)的警告,因为叫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 va
20、riable 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 examplefor表达式(8.8.3)的for初始化器(for-initializer) 和using表达式的资源获取(resource-acquisitio
21、n)可以作为一个隐型局部变量声明。同样,foreach表达式(8.8.4)的迭代变量可以声明为一个隐型局部变量,这种情况下,(隐型局部变量的)类型推导为正被枚举(enumerated)的集合的元素的类型。例子: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的类型推导为numbers的元素类型int。26.2 Extension methods扩展方法Extension methods are static methods that can be invoked using instance method syntax. In effect, extension methods make it possible to extend existing types and constructed types with additional methods.扩展方法是可以通过使用实例方法语法调用的静态方法。效果上,扩展方法使得用附加的方法扩展已存在类型和构造类型成为可
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1