【文章內容簡介】
instance methods. For example, when the namespace from the example above is imported with the usingnamespacedirective 擴展方法用 usingnamespacedirectives (167。)導入。除了導入包含在名字空間中的類型外,usingnamespacedirectives 也導入了名字空間中所有靜態(tài)類中的所有擴展方法。實際上,被導入的擴展方法作為 被修飾的第一個參數類型上的附加方法 出現(xiàn),并且相比常規(guī)實例方法具有較低的優(yōu)先權。比如,當使用 usingnamespacedirective 導入上個例子中 名字空間: using 。 it bees possible to invoke the extension methods in the static class Extensions using instance method syntax: 它使得可以在靜態(tài)類 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 擴展方法調用的詳細規(guī)則表述如下。以如下調用形式之一: 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: 如果調用的正常處理過程發(fā)現(xiàn)沒有適用的實例方法(特別地,如果 這個調用的候選方法集是空的),就會試圖處理擴展方法調用的構造。方法調用會首先被分別重寫稱如下之一: 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. 重寫后的形式然后被作為 靜態(tài)方法調用 處理,除非標識符 identifier 決議為:以最靠近的封閉名字空間聲明開始,以每個封閉名字空間聲明繼續(xù),并以包含的編譯單元結束,持續(xù)地試圖用 組成所有可訪問的,由 usingnamespacedirectives導入的,指明為 identifier名字的擴展方法處理重寫的方法調用。第一個產生非空候選方法集的方法組 (method group)就成為被選中的重寫的方法調用。如果所有的努力都只產生空的候選集,則發(fā)生編譯期錯誤。 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: 前面的規(guī)則標表明實例方法優(yōu)先于擴展方法,并且導入進內層名字空間中的擴展方法優(yōu)先于導入進外層名字空間中的擴展方法。例如: 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的方法優(yōu)先于第一個擴展方法, C的方法優(yōu)先于兩個擴展方法。 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 對象 )被需要的地方以內聯(lián) (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表達式出現(xiàn)的語境推導 —— 特定地,當 lambda表達式被轉型到一個兼容的 delegate 類型時, delegate 類型提供參數的類型 (167。)。 In a lambda expression with a single, implicitly typed parameter, the parentheses may