O'Reilly C# 5.0 in a Nutshell, 5th Edition 9781449320102 Manual Do Utilizador

Códigos do produto
9781449320102
Página de 21
Optional parameters (
allow functions to specify default parameter values
so that callers can omit arguments and named arguments allow a function caller to
identify an argument by name rather than position.
Type variance rules were relaxed in C# 4.0 (Chapters 
 and 
), such that type pa-
rameters in generic interfaces and generic delegates can be marked as covariant or
contravariant, allowing more natural type conversions.
COM interoperability (
was enhanced in C# 4.0 in three ways. First,
arguments can be passed by reference without the 
ref
 keyword (particularly useful
in conjunction with optional parameters). Second, assemblies that contain COM
interop types can be linked rather than referenced. Linked interop types support type
equivalence, avoiding the need for Primary Interop Assemblies and putting an end
to versioning and deployment headaches. Third, functions that return COM-Variant
types from linked interop types are mapped to 
dynamic
 rather than 
object
, elimi-
nating the need for casting.
What’s New in C# 3.0
The features added to C# 3.0 were mostly centered on Language Integrated Query
capabilities or LINQ for short. LINQ enables queries to be written directly within a
C# program and checked statically for correctness, and query both local collections
(such as lists or XML documents) or remote data sources (such as a database). The
C# 3.0 features added to support LINQ comprised implicitly typed local variables,
anonymous types, object initializers, lambda expressions, extension methods, query
expressions and expression trees.
Implicitly typed local variables (
var
 keyword, 
) let you omit the variable
type in a declaration statement, allowing the compiler to infer it. This reduces clutter
as well as allowing anonymous types (
), which are simple classes created
on the fly that are commonly used in the final output of LINQ queries. Arrays can
also be implicitly typed (
).
Object initializers (
simplify object construction by allowing properties to
be set inline after the constructor call. Object initializers work with both named and
anonymous types.
Lambda expressions (
are miniature functions created by the compiler on
the fly, and are particularly useful in “fluent” LINQ queries (
).
Extension methods (
) extend an existing type with new methods (without
altering the type’s definition), making static methods feel like instance methods.
LINQ’s query operators are implemented as extension methods.
Query expressions (
) provide a higher-level syntax for writing LINQ queries
that can be substantially simpler when working with multiple sequences or range
variables.
Expression trees (
) are miniature code DOMs (Document Object Models)
that  describe  lambda  expressions  assigned  to  the  special  type 
Expression<TDele
gate>
. Expression trees make it possible for LINQ queries to execute remotely (e.g.,
What’s New in C# 3.0 | 7
Introduction