- Introduction
- Whats in a Lambda?
- Understanding Closures
- Currying
- Summary
What’s in a Lambda?
A function has a visibility modifier, the function keyword, a method name, and optional parameters and parameter types. Collectively, these are called the function header. A function has a method body and lines of code. All of these elements make up a function. You know what a typical function looks like, so let’s move on.
An anonymous function (also called anonymous method or anonymous delegate) has a consolidated header that has no modifier, includes the delegate keyword, supports optional parameters, and has a method body. Anonymous delegates were roughly defined for those times when only a one-line event handler was needed. Here’s an example of an anonymous delegate from the Visual Studio help documentation:
delegate() { System.Console.Write("Hello, "); System.Console.WriteLine("World!"); } (Honey I shrunk the code!)
Visual Basic didn’t even support anonymous delegates, but that’s another article.
Lambda expressions are even more compact. A Lambda expression has three parts:
- The left side is simply input parameters whose type is inferred, and only parentheses are needed if there’s more than one parameter.
- The second part is what I call the "gosinta" operator (=>).
- The third part is the method body. No return statement is required.
Assume that you want to write a Lambda expression that accepts a string and writes that string to the console. You could write this:
S => Console.WriteLine(S);
Much shorter, right?!
Lambda expressions are really just functions with inputs, and possibly outputs (or return values). You can glean the basic formation of the Lambda expression and assign these to the new generic delegates. For example, the Lambda accepts an input T and then does something. The generic delegate Action<T>(T obj) satisfies the Lambda expression’s signature. The code in Listing 1 shows how you can use the Lambda expression just like a shorter form of an anonymous delegate.
Listing 1 Assigning a Lambda expression to a generic delegate.
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace LambdaExpressions { class Program { static void Main(string[] args) { Action<string> print = s => Console.WriteLine(s); print("Hello, world!"); Console.ReadLine(); } } }
If it helps you wrap your head around Lambda expressions, just think of them as anonymous delegates that were shrunk in your dryer.
The reason that Lambdas have to exist has to do in great part with LINQ. LINQ queries are similar to SQL, and LINQ queries are compact. It wouldn’t be convenient to shoehorn a function or even an anonymous delegate within the confines of a SQL-like query, so something smaller was invented: Lambda expressions.
I don’t have enough time and energy to teach LINQ in this article, and Lambda expressions have other uses such as for arguments to generic delegates and methods like Array.ForEach. (If you want to read about Lambda expressions and LINQ in detail, check out my Sams book LINQ Unleashed for C#, scheduled for publication July 2008.)