C Sharp 3.0
   HOME

TheInfoList



OR:

The programming language C# version 3.0 was released on 19 November 2007 as part of
.NET Framework 3.5 Microsoft started development on the .NET Framework in the late 1990s originally under the name of Next Generation Windows Services (NGWS). By late 2001 the first beta versions of .NET 1.0 were released. The first version of .NET Framework was r ...
. It includes new features inspired by
functional programming In computer science, functional programming is a programming paradigm where programs are constructed by applying and composing functions. It is a declarative programming paradigm in which function definitions are trees of expressions that ...
languages such as
Haskell Haskell () is a general-purpose, statically-typed, purely functional programming language with type inference and lazy evaluation. Designed for teaching, research and industrial applications, Haskell has pioneered a number of programming lan ...
and ML, and is driven largely by the introduction of the
Language Integrated Query Language Integrated Query (LINQ, pronounced "link") is a Microsoft .NET Framework component that adds native data querying capabilities to .NET languages, originally released as a major part of .NET Framework 3.5 in 2007. LINQ extends the langua ...
(LINQ) pattern to the Common Language Runtime. It is not currently standardized by any standards organisation.


C# 3.0 features


LINQ (language-integrated query)

LINQ Language Integrated Query (LINQ, pronounced "link") is a Microsoft .NET Framework component that adds native data querying capabilities to .NET languages, originally released as a major part of .NET Framework 3.5 in 2007. LINQ extends the langu ...
is a new Microsoft-specific extensible, general-purpose query language for many kinds of data sources—including plain object collections, XML documents, databases, etc.—which is tightly integrated with other C# language facilities. The syntax is different from, but borrows from SQL. An example: int[] array = ; // Select squares of all odd numbers in the array sorted in descending order IEnumerable query = from x in array where x % 2

1 orderby x descending select x * x; // Result: 49, 25, 1
To implement LINQ, a large range of new methods were added to many collections via the System.Linq.Enumerable class. LINQ 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. IEnumerable query = array.Where(x => x % 2

1) .OrderByDescending(x => x) .Select(x => x * x); // Result: 49, 25, 1 using 'array' as defined in previous example


Object initializers

Customer c = new Customer(); c.Name = "John"; can be written Customer c = new Customer ;


Collection initializers

MyList list = new MyList(); list.Add(1); list.Add(2); can be written as MyList list = new MyList ; assuming that MyList implements System.Collections.IEnumerable and has a public Add method.


Local variable type inference

Local variable
type inference Type inference refers to the automatic detection of the type of an expression in a formal language. These include programming languages and mathematical type systems, but also natural languages in some branches of computer science and linguistic ...
: var x = new Dictionary>(); is interchangeable with Dictionary> x = new Dictionary>(); This feature is not just a convenient
syntactic sugar In computer science, syntactic sugar is syntax within a programming language that is designed to make things easier to read or to express. It makes the language "sweeter" for human use: things can be expressed more clearly, more concisely, or in an ...
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.


Anonymous types

Anonymous type Anonymous types are a feature of C# 3.0, Visual Basic .NET 9.0, Oxygene, Scala and Go that allows data types to encapsulate a set of properties into a single object without having to first explicitly define a type. This is an important feature ...
s 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. var x = new ; Anonymous types are
reference type In computer programming, data types can be divided into two categories: value types (or by-value types) and reference types (or by-reference types). Value types are completely represented by their meaning, while reference types are references to ano ...
s 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.


Lambda expressions

Lambda Lambda (}, ''lám(b)da'') is the 11th letter of the Greek alphabet, representing the voiced alveolar lateral approximant . In the system of Greek numerals, lambda has a value of 30. Lambda is derived from the Phoenician Lamed . Lambda gave ri ...
expressions provide a concise way to write first-class anonymous function values. Compare the following C# 2.0 snippet: listOfFoo.Where(delegate(Foo x) ); with this C# 3.0 equivalent: listOfFoo.Where(x => x.Size > 10); In the above examples, lambda expressions are merely shorthand 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 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 as x <= y, a = b + c, or even lambda functions and other complex forms can be created dynamically using
expression tree A binary expression tree is a specific kind of a binary tree used to represent expressions. Two common types of expressions that a binary expression tree can represent are algebraic and boolean. These trees can represent expressions that contain ...
s. 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
reflection Reflection or reflexion may refer to: Science and technology * Reflection (physics), a common wave phenomenon ** Specular reflection, reflection from a smooth surface *** Mirror image, a reflection in a mirror or in water ** Signal reflection, in ...
API, this can be a very powerful tool, if a little challenging to write and debug.


Automatic properties

The compiler generates a private instance variable and the appropriate accessor and mutator given code such as: public string Name


Extension methods

Developers may use extension methods to add new methods to the public contract of an existing CLR type, without having to sub-class it or recompile the original type. In reality, extension methods are a form of syntactic sugar that provide the illusion of adding new methods to the existing class outside its definition. The illusion is achieved with the definition of a static method that is callable as if it were an instance method, where the receiver of the call (i.e., the instance) is bound to the first parameter of the method, decorated with keyword this. The requirements for an extension method are as follows: # An extension method must be defined in a static class. # An extension method must be defined as a static method. # An extension method's first parameter must take the following form, where type is the name of the type to be extended: this type parameterName # An extension method may optionally define other parameters to follow the this parameter. This example class demonstrates the definition and use of a Left extension method for strings: public static class StringExtensions string s = "foo bar"; s.Left(3); // same as StringExtensions.Left(s, 3), which returns "foo";


Partial methods

Partial methods allow code generators to generate method declarations as extension points that are only included in the compilation if someone actually implements them in another portion of a partial class.


References

{{reflist


External links


Overview of C# 3.0 (Microsoft MSDN)
C Sharp programming language family