Hi,
This blog post summarizes the new features of C# langauge shipped with Orcas (VS 2005).
Below are the new features been introduced.
§ Automatic Properties
§ Object Initializers
§ Collection Initializers
§ Extesnion Methods
§ Lambda Expressions - p => expressions
§ Query Syntax
§ Anonymous Types
· Concisely define inline CLR types within code, without having to explicitly define a formal class declaration of the type.
· Particularly useful when querying and transforming/projecting/shaping data with LINQ.
§ Var Keyword
Lambda Expression
What is Lambda Expression:
Earliar C# 2.0 introduced the concept of anonymous methods, which allow code blocks to be written "in-line" where delegate values are expected.
Now, Lambda Expressions provide a more concise, functional syntax for writing anonymous methods. They end up being super useful when writing LINQ query expressions - since they provide a very compact and type-safe way to write functions that can be passed as arguments for subsequent evaluation.
Lambda Expression Example:
public class Person
{
public string FirstName { get; set; }
public int Age { get; set; }
public void GetAge()
{
List<Person> people = new List<Person>
{
new Person { FirstName = "Scott1", Age = 32 },
new Person { FirstName = "Scott2", Age = 33 },
new Person { FirstName = "Scott3", Age = 34 }
};
IEnumerable<Person> results = people.Where(p => p.FirstName == "Scott1");
double average = people.Average(p => p.Age);
}
}
The p => expressions used above are Lambda expressions.
In C# a lambda expression is syntactically written as a parameter list, followed by a => token, and then followed by the expression or statement block to execute when the expression is invoked:
params => expression
So when we wrote the lambda expression:
p => p.LastName == "Guthrie"
we were indicating that the Lambda we were defining took a parameter "p", and that the expression of code to run returns whether the p.LastName value equals "Guthrie". The fact that we named the parameter "p" is irrelevant - I could just have easily named it "o", "x", "foo" or any other name I wanted.
The actual code for above using C# 2.0 anonymous methods would be like below:
public void GetAge()
{
List<Person> people = new List<Person>
{
new Person { FirstName = "Scott1", Age = 32 },
new Person { FirstName = "Scott2", Age = 33 },
new Person { FirstName = "Scott3", Age = 34 }
};
IEnumerable<Person> results = people.Where(
delegate(Person p)
{
return p.FirstName == "Scott1";
}
);
double average = people.Average(
delegate(Person p)
{
return p.Age;
}
);
}
What is different than Anonymous Methods:
Unlike anonymous methods, which require parameter type declarations to be explicitly stated, Lambda expressions permit parameter types to be omitted and instead allow them to be inferred based on the usage. For example, when we wrote the lambda expression p=>p.LastName == "Guthrie", the compiler inferred that the p parameter was of type Person because the "Where" extension method was working on a generic List<Person> collection.
Lambda parameter types can be inferred at both compile-time and by the Visual Studio's intellisense.
Note: We can also explicitly declare the type of a parameter to a Lambda expression as below.
IEnumerable<Person> results = people.Where(Person p => p.FirstName == "Scott1");
Advanced: Lambda Expression Trees for Framework Developers
One of the things that make Lambda expressions particularly powerful from a framework developer's perspective is that they can be compiled as either a code delegate (in the form of an IL based method) or as an Expression Tree Object which can be used at runtime to Analyze, Transform Or Optimize the expression.
This ability to compile a Lambda expression to an expression tree object is an Extremely Powerful Mechanism that enables a host of scenarios - including the ability to build High Performance Object Mappers that support rich querying of data (whether from a relational database, an active directory, a web-service, etc) using a consistent query language that provides compile-time syntax checking and VS intellisense.
To read more on it refer the post.
Reference: http://weblogs.asp.net/scottgu/archive/2007/04/08/new-orcas-language-feature-lambda-expressions.aspx
Hope this helps.
Thanks & Regards,
Arun Manglick || Tech Lead
No comments:
Post a Comment