c30规范(编辑修改稿)内容摘要:

instance methods. For example, when the namespace from the example above is imported with the usingnamespacedirective 扩展方法用 usingnamespacedirectives (167。 )导入。 除了导入包含在名字空间中的类型外,usingnamespacedirectives 也导入了名字空间中所有静态类中的所有扩展方法。 实际上,被导入的扩展方法作为 被修饰的第一个参数类型上的附加方法 出现,并且相比常规实例方法具有较低的优先权。 比如,当使用 usingnamespacedirective 导入上个例子中 名字空间: using。 it bees possible to invoke the extension methods in the static class Extensions using instance method syntax: 它使得可以在静态类 Extension上使用实例方法语法调用扩展方法: string s = 1234。 int i = ()。 // Same as (s) int[] digits = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}。 int[] a = (4, 3)。 // Same as (digits, 4, 3) Extension method invocations扩展方法调用 The detailed rules for extension method invocation are described in the following. In a method invocation (167。 ) of one of the forms 扩展方法调用的详细规则表述如下。 以如下调用形式之一: expr . identifier ( ) expr . identifier ( args ) expr . identifier typeargs ( ) expr . identifier typeargs ( args ) if the normal processing of the invocation finds no applicable instance methods (specifically, if the set of candidate methods for the invocation is empty), an attempt is made to process the construct as an extension method invocation. The method invocation is first rewritten to one of the following, respectively: 如果调用的正常处理过程发现没有适用的实例方法(特别地,如果 这个调用的候选方法集是空的),就会试图处理扩展方法调用的构造。 方法调用会首先被分别重写称如下之一: identifier ( expr ) identifier ( expr , args ) identifier typeargs ( expr ) identifier typeargs ( expr , args ) The rewritten form is then processed as a static method invocation, except for the way in which identifier is resolved: Starting with the closest enclosing namespace declaration, continuing with each enclosing namespace declaration, and ending with the containing pilation unit, successive attempts are made to process the rewritten method invocation with a method group consisting of all accessible extension methods with the name given by identifier imported by the namespace declaration’s usingnamespacedirectives. The first method group that yields a nonempty set of candidate methods is the one chosen for the rewritten method invocation. If all attempts yield empty sets of candidate methods, a piletime error occurs. 重写后的形式然后被作为 静态方法调用 处理,除非标识符 identifier 决议为:以最靠近的封闭名字空间声明开始,以每个封闭名字空间声明继续,并以包含的编译单元结束,持续地试图用 组成所有可访问的,由 usingnamespacedirectives导入的,指明为 identifier名字的扩展方法处理重写的方法调用。 第一个产生非空候选方法集的方法组 (method group)就成为被选中的重写的方法调用。 如果所有的努力都只产生空的候选集,则发生编译期错误。 The preceeding rules mean that instance methods take precedence over extension methods, and extension methods imported in inner namespace declarations take precedence over extension methods imported in outer namespace declarations. For example: 前面的规则标表明实例方法优先于扩展方法,并且导入进内层名字空间中的扩展方法优先于导入进外层名字空间中的扩展方法。 例如: using N1。 namespace N1 { public static class E { public static void F(this object obj, int i) { } public static void F(this object obj, string s) { } } } class A { } class B { public void F(int i) { } } class C { public void F(object obj) { } } class X { static void Test(A a, B b, C c) { (1)。 // (object, int) (hello)。 // (object, string) (1)。 // (int) (hello)。 // (object, string) (1)。 // (object) (hello)。 // (object) } } In the example, B’s method takes precedence over the first extension method, and C’s method takes precedence over both extension methods. 例子中, B的方法优先于第一个扩展方法, C的方法优先于两个扩展方法。 Lambda expressions Lambda表达式 C introduces anonymous methods, which allow code blocks to be written “inline” where delegate values are expected. While anonymous methods provide much of the expressive power of functional programming languages, the anonymous method syntax is rather verbose and imperative in nature. Lambda expressions provide a more concise, functional syntax for writing anonymous methods. C 引入了匿名方法,它允许在 delegate 值 (delegate value) (译者 :delegate 对象 )被需要的地方以内联 (inline)方式写一个代码块。 当匿名方法提供了大量函数式编程语言 (或泛函编程 )(functional programming)的表达力时,实质上,匿名方法的语法是相当烦琐和带有强制性的。 Lambda表达式提供了一个更加简练的函数式语法来写匿名方法。 A lambda expression is written as a parameter list, followed by the = token, followed by an expression or a statement block. Lambda表达式写成一个后面紧跟 = 标记的参数列表, =之后是一个表达式或表语句块。 expression: assignment nonassignmentexpression nonassignmentexpression: conditionalexpression lambdaexpression queryexpression lambdaexpression: ( lambdaparameterlistopt ) = lambdaexpressionbody implicitlytypedlambdaparameter = lambdaexpressionbody lambdaparameterlist: explicitlytypedlambdaparameterlist implicitlytypedlambdaparameterlist explicitlytypedlambdaparameterlist explicitlytypedlambdaparameter explicitlytypedlambdaparameterlist , explicitlytypedlambdaparameter explicitlytypedlambdaparameter: parametermodifieropt type identifier implicitlytypedlambdaparameterlist implicitlytypedlambdaparameter implicitlytypedlambdaparameterlist , implicitlytypedlambdaparameter implicitlytypedlambdaparameter: identifier lambdaexpressionbody: expression block The parameters of a lambda expression can be explicitly or implicitly typed. In an explicitly typed parameter list, the type of each parameter is explicitly stated. In an implicitly typed parameter list, the types of the parameters are inferred from the context in which the lambda expression occurs—specifically, when the lambda expression is converted to a patible delegate type, that delegate type provides the parameter types (167。 ). Lambda表达式的参数可以是显型和隐型的。 在显型参数列表中,每个参数的类型是显式指定的。 在隐型参数列表中,参数的类型由 lambda表达式出现的语境推导 —— 特定地,当 lambda表达式被转型到一个兼容的 delegate 类型时, delegate 类型提供参数的类型 (167。 )。 In a lambda expression with a single, implicitly typed parameter, the parentheses may。
阅读剩余 0%
本站所有文章资讯、展示的图片素材等内容均为注册用户上传(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考。 用户通过本站上传、发布的任何内容的知识产权归属用户或原始著作权人所有。如有侵犯您的版权,请联系我们反馈本站将在三个工作日内改正。