c#30中英文Word格式.docx

上传人:b****6 文档编号:20698049 上传时间:2023-01-25 格式:DOCX 页数:38 大小:43.92KB
下载 相关 举报
c#30中英文Word格式.docx_第1页
第1页 / 共38页
c#30中英文Word格式.docx_第2页
第2页 / 共38页
c#30中英文Word格式.docx_第3页
第3页 / 共38页
c#30中英文Word格式.docx_第4页
第4页 / 共38页
c#30中英文Word格式.docx_第5页
第5页 / 共38页
点击查看更多>>
下载资源
资源描述

c#30中英文Word格式.docx

《c#30中英文Word格式.docx》由会员分享,可在线阅读,更多相关《c#30中英文Word格式.docx(38页珍藏版)》请在冰豆网上搜索。

c#30中英文Word格式.docx

26.2Extensionmethods扩展方法............................................................................................................6

26.2.1Declaringextensionmethods 

 

声明扩展方法................................................................................6

26.2.2Importingextensionmethods 

导入扩展方法................................................................................7

26.2.3Extensionmethodinvocations 

扩展方法的调用...........................................................................7

26.3Lambdaexpressions 

Lambda表达式.................................................................................................8

26.3.1Lambdaexpressionconversions 

Lambda表达式转换................................................................10

26.3.2Typeinference 

类型推导.........................................................................................................11

26.3.3Overloadresolution 

重载决议...................................................................................................13

26.4Objectandcollectioninitializers 

对象和集合的初始化器.................................................................14

26.4.1Objectinitializers 

对象初始化器...............................................................................................14

26.4.2Collectioninitializers 

集合初始化器..........................................................................................16

26.5Anonymoustypes 

匿名类型............................................................................................................17

26.6Implicitlytypedarrays 

隐型数组(Implicitlytypedarrays)..................................................................18

26.7Queryexpressions 

查询表达式.......................................................................................................19

26.7.1Queryexpressiontranslation 

查询表达式的转换.......................................................................20

26.7.1.1whereclauseswhere子句...................................................................................................21

26.7.1.2selectclausesselect子句...................................................................................................21

26.7.1.3groupclausesgroup子句.....................................................................................................21

26.7.1.4orderbyclausesorderby子句..............................................................................................22

26.7.1.5Multiplegenerators多重产生器(generator).......................................................................22

26.7.1.6intoclauses 

info子句..........................................................................................................23

26.7.2Thequeryexpressionpattern 

查询表达式模式..........................................................................23

26.7.3Formaltranslationrules 

正式的转换规则..................................................................................24

26.8Expressiontrees 

表达式树............................................................................................................26

26.OverviewofC#3.0(C#3.0概述)

C#3.0(“C#Orcas”)introducesseverallanguageextensionsthatbuildonC#2.0tosupportthecreationanduseofhigherorder,functionalstyleclasslibraries.TheextensionsenableconstructionofcompositionalAPIsthathaveequalexpressivepowerofquerylanguagesindomainssuchasrelationaldatabasesandXML.Theextensionsinclude:

C#3.0(“C#魔兽(Orcas)”)引入了几个构建在C#2.0上的语言扩展,用来支持创建和使用更高级的函数式(functional或译:

泛函)类库。

这些扩展允许组合(compositional)APIs的构造,这些APIs与关系数据库和XML等领域中的查询语言具有同等的表达力。

∙ 

Implicitlytypedlocalvariables,whichpermitthetypeoflocalvariablestobeinferredfromtheexpressionsusedtoinitializethem.

隐型局部变量,允许局部变量的类型从初始化它们的表达式推导而来。

Extensionmethods,whichmakeitpossibletoextendexistingtypesandconstructedtypeswithadditionalmethods.

扩展方法,使得使用附加(additional)的方法扩展已存在的类型和构造类型成为可能。

Lambdaexpressions,anevolutionofanonymousmethodsthatprovidesimprovedtypeinferenceandconversionstobothdelegatetypesandexpressiontrees.

Lambda表达式,是匿名方法的演进,可提供改良的类型推导和到dalegate类型和表达式树的转换。

Objectinitializers,whicheaseconstructionandinitializationofobjects.

对象初始化器,简化了对象的构造和初始化。

Anonymoustypes,whicharetupletypesautomaticallyinferredandcreatedfromobjectinitializers.

匿名类型,是从对象初始化器自动推导和创建的元组(tuple)类型。

Implicitlytypedarrays,aformofarraycreationandinitializationthatinferstheelementtypeofthearrayfromanarrayinitializer.

隐型数组,数组创建和初始化的形式,它从数组初始化器推导出数组的元素类型。

Queryexpressions,whichprovidealanguageintegratedsyntaxforqueriesthatissimilartorelationalandhierarchicalquerylanguagessuchasSQLandXQuery.

查询表达式,为类似于关系型和层次化查询语言(比如SQL和XQuery)提供一个语言集成(intergrated)的语法。

Expressiontrees,whichpermitlambdaexpressionstoberepresentedasdata(expressiontrees)insteadofascode(delegates).

表达式树,允许lambda表达式表示为数据(表达式树)而不是代码(delegate)。

Thisdocumentisatechnicaloverviewofthosefeatures.ThedocumentmakesreferencetotheC#LanguageSpecification1.2(§

1through§

18)andtheC#LanguageSpecification2.0(§

19through§

25),bothofwhichareavailableontheC#LanguageHomePage(

本文档是这些特征的技术概述。

文档引用了C#语言规范1.2(§

1-§

18)和C#语言规范2.0(§

19-§

25),这两个规范都在C#语言主页上(

26.1Implicitlytypedlocalvariables隐型局部变量

Inanimplicitlytypedlocalvariabledeclaration,thetypeofthelocalvariablebeingdeclaredisinferredfromtheexpressionusedtoinitializethevariable.Whenalocalvariabledeclarationspecifiesvarasthetypeandnotypenamedvarisinscope,thedeclarationisanimplicitlytypedlocalvariabledeclaration.Forexample:

在隐型局部变量声明中,正被声明的局部变量的类型从初始化这个变量的表达式推导得来。

当局部变量声明指明var作为类型,并且该范围域(scope)中没有var名称的类型存在,这个声明就称为隐型局部声明。

例如:

vari=5;

vars="

Hello"

;

vard=1.0;

varnumbers=newint[]{1,2,3};

varorders=newDictionary<

int,Order>

();

Theimplicitlytypedlocalvariabledeclarationsabovearepreciselyequivalenttothefollowingexplicitlytypeddeclarations:

上面的隐型局部变量声明精确地等同于下面的显型(explicitlytyped)声明:

inti=5;

strings="

doubled=1.0;

int[]numbers=newint[]{1,2,3};

Dictionary<

orders=newDictionary<

Alocalvariabledeclaratorinanimplicitlytypedlocalvariabledeclarationissubjecttothefollowingrestrictions:

隐型局部变量声明中的局部变量声明符(declarator)遵从下面这些约束:

Thedeclaratormustincludeaninitializer.

声明符必须包含初始化器。

Theinitializermustbeanexpression.Theinitializercannotbeanobjectorcollectioninitializer(§

26.4)byitself,butitcanbeanewexpressionthatincludesanobjectorcollectioninitializer.

初始化器必须是一个表达式。

初始化器不能是一个自身的对象或者集合初始化器(26.4),但是它可以是包含一个对象或集合初始化器的一个new表达式。

Thecompile-timetypeoftheinitializerexpressioncannotbethenulltype.

初始化器表达式的编译期类型不可以是空(null)类型。

Ifthelocalvariabledeclarationincludesmultipledeclarators,theinitializersmustallhavethesamecompile-timetype.

如果局部变量声明包含了多个声明符,这些声明符必须具备同样的编译期类型。

Thefollowingareexamplesofincorrectimplicitlytypedlocalvariabledeclarations:

下面是一些不正确的隐型局部变量声明的例子:

varx;

//Error,noinitializertoinfertypefrom

vary={1,2,3};

//Error,collectioninitializernotpermitted

varz=null;

//Error,nulltypenotpermitted

Forreasonsofbackwardcompatibility,whenalocalvariabledeclarationspecifiesvarasthetypeandatypenamedvarisinscope,thedeclarationreferstothattype;

however,awarningisgeneratedtocallattentiontotheambiguity.Sinceatypenamedvarviolatestheestablishedconventionofstartingtypenameswithanuppercaseletter,thissituationisunlikelytooccur.

因为向后兼容的原因,当局部变量声明指定var作为类型,而范围域中又存在叫var的类型,则这个声明会推导为那个叫var的类型;

然后,会产生一个关注含糊性(ambiguity)的警告,因为叫var的类型违反了既定的类名首字母大写的约定,这个情形也未必会出现。

(译者:

视编译器实现而定)

Thefor-initializerofaforstatement(§

8.8.3)andtheresource-acquisitionofausingstatement(§

8.13)canbeanimplicitlytypedlocalvariabledeclaration.Likewise,theiterationvariableofaforeachstatement(§

8.8.4)maybedeclaredasanimplicitlytypedlocalvariable,inwhichcasethetypeoftheiterationvariableisinferredtobetheelementtypeofthecollectionbeingenumerated.Intheexample

for表达式(§

8.8.3)的for初始化器(for-initializer)和using表达式的资源获取(resource-acquisition)可以作为一个隐型局部变量声明。

同样,foreach表达式(§

8.8.4)的迭代变量可以声明为一个隐型局部变量,这种情况下,(隐型局部变量的)类型推导为正被枚举(enumerated)的集合的元素的类型。

例子:

int[]numbers={1,3,5,7,9};

foreach(varninnumbers)Console.WriteLine(n);

thetypeofnisinferredtobeint,theelementtypeofnumbers.

n的类型推导为numbers的元素类型int。

26.2Extensionmethods扩展方法

Extensionmethodsarestaticmethodsthatcanbeinvokedusinginstancemethodsyntax.Ineffect,extensionmethodsmakeitpossibletoextendexistingtypesandconstructedtypeswithadditionalmethods.

扩展方法是可以通过使用实例方法语法调用的静态方法。

效果上,扩展方法使得用附加的方法扩展已存在类型和构造类型成为可

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 经管营销 > 经济市场

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1