Csharp 3.0 specification

Solo disponible en BuenasTareas
  • Páginas : 30 (7475 palabras )
  • Descarga(s) : 4
  • Publicado : 19 de mayo de 2010
Leer documento completo
Vista previa del texto

Version 3.0 Specification

September 2005


© 2005 Microsoft Corporation. All rights reserved.

Microsoft, Windows, Visual Basic, Visual C#, and Visual C++ are either registered trademarks or trademarks of Microsoft Corporation in the U.S.A. and/or other countries/regions.

Other product and company names mentioned herein may be the trademarks of their respective owners.Table of Contents

26. Overview of C# 3.0 5
26.1 Implicitly typed local variables 5
26.2 Extension methods 6
26.2.1 Declaring extension methods 6
26.2.2 Importing extension methods 7
26.2.3 Extension method invocations 7
26.3 Lambda expressions 8
26.3.1 Lambda expression conversions 10
26.3.2 Type inference 11
26.3.3 Overload resolution 13
26.4 Object andcollection initializers 14
26.4.1 Object initializers 14
26.4.2 Collection initializers 16
26.5 Anonymous types 17
26.6 Implicitly typed arrays 18
26.7 Query expressions 19
26.7.1 Query expression translation 20 where clauses 21 select clauses 21 group clauses 21 orderby clauses 22 Multiple generators22 into clauses 23
26.7.2 The query expression pattern 23
26.7.3 Formal translation rules 24
26.8 Expression trees 26
Overview of 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 compositional APIs that haveequal expressive power of query languages in domains such as relational databases and XML. The extensions include:

• Implicitly typed 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.

• Lambdaexpressions, an evolution of anonymous methods that provides improved type inference and conversions to both delegate types and expression trees.

• Object initializers, which ease construction and initialization of objects.

• Anonymous types, which are tuple types automatically inferred and created from object initializers.

• Implicitly typed arrays, a form of array creation and initializationthat infers the element type of the array from an array initializer.

• Query expressions, which provide a language integrated syntax for queries that is similar to relational and hierarchical query languages such as SQL and XQuery.

• Expression trees, which permit lambda expressions to be represented as data (expression trees) instead of as code (delegates).

This document is atechnical 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 through §25), both of which are available on the C# Language Home Page (http://msdn.microsoft.com/vcsharp/language).

1 Implicitly typed local variables

In an implicitly typed local variable declaration, the type of the local variablebeing declared is inferred from the expression 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 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:

int i = 5;
string s = "Hello";
double d = 1.0;
int[] numbers = new int[] {1, 2, 3};
Dictionary orders = new Dictionary();

A local variable declarator in an implicitly typed local variable declaration is subject to the following...
tracking img