C Sharp 3.0
Encyclopedia
The programming language C# version 3.0 was released on 19 November 2007 as part of .NET Framework 3.5. It includes new features inspired by functional programming
languages such as Haskell
and ML, and is driven largely by the introduction of the Language Integrated Query
(LINQ) pattern to the Common Language Runtime. It is not currently standardized by any standards organisation.
is a new Microsoft-specific extensible, general-purpose query language for many kinds of data sources (including plain object collections, XML documents, databases and so on) which was tightly integrated with other C# language facilities. The syntax is different from, but borrows from SQL
. An example:
In order to implement LINQ, a large range of new methods were added to many collections via the
can be written
can be written as
assuming that
Anonymous types are reference types that derive directly from object. The compiler gives them a name although your application cannot access it. From the perspective of the common language runtime, an anonymous type is no different from any other reference type, except that it cannot be cast to any type except for object.
If two or more anonymous types have the same number and type of properties in the same order, the compiler treats them as the same type and they share the same compiler-generated type information.
:
is interchangeable with
This feature is not just a convenient syntactic sugar
for shorter local variable declarations, but it is also required for the declaration of variables of anonymous types. The contextual keyword "var", however, may only appear within a local variable declaration.
expressions provide a concise way to write first-class anonymous function values. Compare the following C# 2.0 snippet:
with this C# 3.0 equivalent:
In the above examples, lambda expressions are merely short-hand syntax for anonymous delegates with type inference for parameters and return type. However, depending on the context they are used in, a C# compiler can also transform lambdas into ASTs
that can then be processed at run-time. In the example above, if
API, this can be a very powerful tool, if a little challenging to write and debug.
Extension methods allow developers to add new methods to the public contract of an existing CLR type, without having to sub-class it or recompile the original type.
1. All Extension Methods need to be defined in a static class.
2. All Extension Methods need to be defined as a static method.
3. All Extension Methods need to take at least one parameter defined as follows:
Where the type is whatever type that you wish to extend.
4. The
Functional programming
In computer science, functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data. It emphasizes the application of functions, in contrast to the imperative programming style, which emphasizes changes in state...
languages such as Haskell
Haskell (programming language)
Haskell is a standardized, general-purpose purely functional programming language, with non-strict semantics and strong static typing. It is named after logician Haskell Curry. In Haskell, "a function is a first-class citizen" of the programming language. As a functional programming language, the...
and ML, and is driven largely by the introduction of the Language Integrated Query
Language Integrated Query
Language Integrated Query is a Microsoft .NET Framework component that adds native data querying capabilities to .NET languages, although ports exist for Java, PHP and JavaScript....
(LINQ) pattern to the Common Language Runtime. It is not currently standardized by any standards organisation.
LINQ (Language-Integrated Query)
LINQLINQ
Linq is a word-based card game from Endless Games, introduced at the American International Toy Fair in 2005.Game play requires at least four players, two of whom are dealt cards with the same word, while the others receive blanks. The goal is to gain points by correctly naming the players with...
is a new Microsoft-specific extensible, general-purpose query language for many kinds of data sources (including plain object collections, XML documents, databases and so on) which was tightly integrated with other C# language facilities. The syntax is different from, but borrows from SQL
SQL
SQL is a programming language designed for managing data in relational database management systems ....
. An example:
In order to implement LINQ, a large range of new methods were added to many collections via the
System.Linq.Enumerable
class. LINQ query language expressions are translated to use these functions before compilation. As an alternative, which is sometimes more powerful or direct, these functions may be accessed directly. Doing so makes more use of lambda functions, which are discussed below. The following is functionally identical to the example above.Object initializers
can be written
Collection initializers
can be written as
assuming that
MyList
implements System.Collections.IEnumerable
and has a public Add
method. Anonymous types
Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to first explicitly define a type. The type name is generated by the compiler and is not available at the source code level. The type of the properties is inferred by the compiler.Anonymous types are reference types that derive directly from object. The compiler gives them a name although your application cannot access it. From the perspective of the common language runtime, an anonymous type is no different from any other reference type, except that it cannot be cast to any type except for object.
If two or more anonymous types have the same number and type of properties in the same order, the compiler treats them as the same type and they share the same compiler-generated type information.
Local variable type inference
Local variable type inferenceType inference
Type inference refers to the automatic deduction of the type of an expression in a programming language. If some, but not all, type annotations are already present it is referred to as type reconstruction....
:
is interchangeable with
This feature is not just a convenient syntactic sugar
Syntactic sugar
Syntactic sugar is a computer science term that refers to syntax within a programming language that is designed to make things easier to read or to express....
for shorter local variable declarations, but it is also required for the declaration of variables of anonymous types. The contextual keyword "var", however, may only appear within a local variable declaration.
Lambda expressions
LambdaClosure (computer science)
In computer science, a closure is a function together with a referencing environment for the non-local variables of that function. A closure allows a function to access variables outside its typical scope. Such a function is said to be "closed over" its free variables...
expressions provide a concise way to write first-class anonymous function values. Compare the following C# 2.0 snippet:
with this C# 3.0 equivalent:
In the above examples, lambda expressions are merely short-hand syntax for anonymous delegates with type inference for parameters and return type. However, depending on the context they are used in, a C# compiler can also transform lambdas into ASTs
Abstract syntax tree
In computer science, an abstract syntax tree , or just syntax tree, is a tree representation of the abstract syntactic structure of source code written in a programming language. Each node of the tree denotes a construct occurring in the source code. The syntax is 'abstract' in the sense that it...
that can then be processed at run-time. In the example above, if
listOfFoo
is not a plain in-memory collection, but a wrapper around a database table, it could use this technique to translate the body of the lambda into the equivalent SQL expression for optimized execution. Either way, the lambda expression itself looks exactly the same in the code, so the way it is used at run-time is transparent to the client.Expression trees
Expressions, such asx <= y
, a = b + c
, or even lambda functions and other complex forms can be created dynamically using expression trees. Much of the functionality is provided by static methods of the class System.Linq.Expressions.Expression
. There are also various new classes in that namespace that represent the expressions and partial expressions created by those methods as software objects. These include BinaryExpression
, which could represent x <= y
; LambdaExpression
and many others. When combined with aspects of the reflectionReflection (computer science)
In computer science, reflection is the process by which a computer program can observe and modify its own structure and behavior at runtime....
API, this can be a very powerful tool, if a little challenging to write and debug.
Automatic properties
The compiler will generate a private instance variable and the appropriate accessor and mutator given code such as:Extension methods
Extension methods are a form of syntactic sugar providing the illusion of adding new methods to the existing class outside its definition. In practice, an extension method is a static method that is callable as if it was an instance method; the receiver of the call is bound to the first parameter of the method, decorated with keywordthis
:Extension methods allow developers to add new methods to the public contract of an existing CLR type, without having to sub-class it or recompile the original type.
1. All Extension Methods need to be defined in a static class.
2. All Extension Methods need to be defined as a static method.
3. All Extension Methods need to take at least one parameter defined as follows:
this
(keyword) string (type) parameterNameWhere the type is whatever type that you wish to extend.
4. The
this
parameter needs to be the first parameter. All other parameters are optional—and must follow this
.