【文章內(nèi)容簡(jiǎn)介】
instance methods. For example, when the namespace from the example above is imported with the usingnamespacedirective 擴(kuò)展方法用 usingnamespacedirectives (167。)導(dǎo)入。除了導(dǎo)入包含在名字空間中的類(lèi)型外,usingnamespacedirectives 也導(dǎo)入了名字空間中所有靜態(tài)類(lèi)中的所有擴(kuò)展方法。實(shí)際上,被導(dǎo)入的擴(kuò)展方法作為 被修飾的第一個(gè)參數(shù)類(lèi)型上的附加方法 出現(xiàn),并且相比常規(guī)實(shí)例方法具有較低的優(yōu)先權(quán)。比如,當(dāng)使用 usingnamespacedirective 導(dǎo)入上個(gè)例子中 名字空間: using 。 it bees possible to invoke the extension methods in the static class Extensions using instance method syntax: 它使得可以在靜態(tài)類(lèi) Extension上使用實(shí)例方法語(yǔ)法調(diào)用擴(kuò)展方法: 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擴(kuò)展方法調(diào)用 The detailed rules for extension method invocation are described in the following. In a method invocation (167。) of one of the forms 擴(kuò)展方法調(diào)用的詳細(xì)規(guī)則表述如下。以如下調(diào)用形式之一: 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: 如果調(diào)用的正常處理過(guò)程發(fā)現(xiàn)沒(méi)有適用的實(shí)例方法(特別地,如果 這個(gè)調(diào)用的候選方法集是空的),就會(huì)試圖處理擴(kuò)展方法調(diào)用的構(gòu)造。方法調(diào)用會(huì)首先被分別重寫(xiě)稱如下之一: 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. 重寫(xiě)后的形式然后被作為 靜態(tài)方法調(diào)用 處理,除非標(biāo)識(shí)符 identifier 決議為:以最靠近的封閉名字空間聲明開(kāi)始,以每個(gè)封閉名字空間聲明繼續(xù),并以包含的編譯單元結(jié)束,持續(xù)地試圖用 組成所有可訪問(wèn)的,由 usingnamespacedirectives導(dǎo)入的,指明為 identifier名字的擴(kuò)展方法處理重寫(xiě)的方法調(diào)用。第一個(gè)產(chǎn)生非空候選方法集的方法組 (method group)就成為被選中的重寫(xiě)的方法調(diào)用。如果所有的努力都只產(chǎn)生空的候選集,則發(fā)生編譯期錯(cuò)誤。 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ī)則標(biāo)表明實(shí)例方法優(yōu)先于擴(kuò)展方法,并且導(dǎo)入進(jìn)內(nèi)層名字空間中的擴(kuò)展方法優(yōu)先于導(dǎo)入進(jìn)外層名字空間中的擴(kuò)展方法。例如: 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)先于第一個(gè)擴(kuò)展方法, C的方法優(yōu)先于兩個(gè)擴(kuò)展方法。 Lambda expressions Lambda表達(dá)式 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 對(duì)象 )被需要的地方以內(nèi)聯(lián) (inline)方式寫(xiě)一個(gè)代碼塊。當(dāng)匿名方法提供了大量函數(shù)式編程語(yǔ)言 (或泛函編程 )(functional programming)的表達(dá)力時(shí),實(shí)質(zhì)上,匿名方法的語(yǔ)法是相當(dāng)煩瑣和帶有強(qiáng)制性的。Lambda表達(dá)式提供了一個(gè)更加簡(jiǎn)練的函數(shù)式語(yǔ)法來(lái)寫(xiě)匿名方法。 A lambda expression is written as a parameter list, followed by the = token, followed by an expression or a statement block. Lambda表達(dá)式寫(xiě)成一個(gè)后面緊跟 = 標(biāo)記的參數(shù)列表, =之后是一個(gè)表達(dá)式或表語(yǔ)句塊。 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表達(dá)式的參數(shù)可以是顯型和隱型的。在顯型參數(shù)列表中,每個(gè)參數(shù)的類(lèi)型是顯式指定的。在隱型參數(shù)列表中,參數(shù)的類(lèi)型由 lambda表達(dá)式出現(xiàn)的語(yǔ)境推導(dǎo) —— 特定地,當(dāng) lambda表達(dá)式被轉(zhuǎn)型到一個(gè)兼容的 delegate 類(lèi)型時(shí), delegate 類(lèi)型提供參數(shù)的類(lèi)型 (167。)。 In a lambda expression with a single, implicitly typed parameter, the parentheses may