Λ is more than the 11th letter of the Greek alphabet.
C# in .NET 3.0 got a number of handy new language features, but the one I’m enjoying the most at the moment is the addition of the => operator (otherwise know as lambda). While this operator does not add any new functionality to the language, it is a great piece of syntactic sugar.
In .NET 2.0, writing anonymous functions inline was a very verbose process, you might often had a hard time reading them ’cause they were looking kinda ugly.
To show you what I do mean I created a simple function. It takes a delegate that should take in 2 integers and return an integer value. Additionally it takes a list of intergers (params is used here). Finally it returns a single integer.
public delegate int expo(int x, int y);
public int SomeFunc(expo f, params int[] list)
{
int r = 1;
foreach (int i in list)
r = f(r, i);
return r;
}
This function is pretty clear, but the usage is ugly:
int n = SomeFunc(new expo(
delegate(int x, int y) { return x * y; }), 55, 33, 223, 31);
also that is not even better:
int n = SomeFunc(delegate(int x, int y) { return x * y; },
55, 33, 223, 31);
But now have a look at that great solution:
int n = SomeFunc((x, y) => x * y, 55, 33, 223, 31);
That’s an example of a lambda expression. Lambda expressions are always of this form:
parameters => expression
You no longer need to use the word delegate, you no longer need to declare the types of the parameters to the function.
This is just the basic use of the new lamdba operator in C#. So far, lambdas don’t appear to offer anything I didn’t already have with anonymous methods besides a different syntax. However, they turn out to be able to do something that anonymous methods cannot.
Indeed most of the new C# 3.0 languages features have been added on LINQ’s behalf, although none of them is inextricably bound to LINQ.
LINQ (Language Integrated Query) defines a standard way of performing queries against various types of data in a way that can be tightly integrated into a language. I wrote about that earlier.
Consider the following code which is very common:
DataContext dc = new DataContext("...");
Table<Cars> cars = dc.GetTable<Cars>();
Table<Brands> brands = dc.GetTable<Brands>();
var q = from c in cars, b in brands
where b.Name == "BMW" && (c.ID == b.ID)
select new { b.Date, c.CarHolder, c.Street, };
foreach (var item in q)
{
Print(String.Format("Date: {0} Belongs to: {1} at {2}",
item.Date, item.CarHolder, item.Street));
}
The way I’d prefer when using lambda is:
var q = cars.Where(b => b.Name == "BMW").SelectMany(
b => brands.Where(b => c.ID == b.ID).
Select(b => new { b.Date, c.CarHolder, c.Street }));
Deferred execution allows the language extensions for XLinq, DLinq, LINQ, or any other Linq extension that comes after to process the logic represented by the expression tree before execution so that optimizations can take place.
If you are planning on doing anything with LINQ, you should know what a lambda is, how to use it in real terms.