Lambda expressions introduction
Software Development September 7th, 2007
NET 2.0 introduced Anonymous Methods. The idea behind anonymous methods it to write methods inline to the code. They are mainly used for small methods that don’t require any need for reuse. For example, we have this code:
1: bool isGreaterThan5(int n)
2: {
3: return n > 5;
4: }
5:
6: ...
7:
8: int GetGreatersThanFives(List<int> numbers)
9: {
10: return numbers.Find(isGreaterThan5);
11: }
We can code it with anonymous method:
1: int GetGreaterThanFives(List<int> numbers)
2: {
3: return numbers.Find(
4: delegate(int n) { return n > 5; }
5: );
6: }
c# 3.0 represent cool thing: Lambda expressions.
Lambda Expressions make things easier by allowing you to avoid the anonymous method and that annoying statement block.
Instead
1: delegate(int n){ return n > 5; }
We can write:
1: n => n > 5
The form of Lambda expressions is:
argument-list => expression
Parameters to Lambda Expressions
The parameters of a lambda expression can be explicitly or implicitly typed. In an explicitly typed parameter list, the type of each expression is explicitly specified. In an implicitly typed parameter list, the types are inferred from the context in which the lambda expression occurs:
1: (int x) => x + 100 // explicitly typed parameter
2:
3: (x,y) => return x * y; // implicitly typed parameter
Lambda Expressions with Multiple Parameters
1: (x, y) => return x * y
Lambda Expressions without Parameters
1: () => Console.WriteLine("Hello world!") // No parameters
I think that’s enough intro to Lambda Expressions.
I will follow up with a part 2 to show more about this cool fteature: Expression Trees, how LINQ uses it to pass code as parameters and more.
Similar Posts:
- C# 3.0 Extension Methods
- Code Analysis in VS 2008
- RegEx for data generation in VSTS DBPro
- RegEx for data generation in VSTS DBPro
- How to: Calling Web Services methods from Client Script in ASP.NET AJAX
Tags: .NET 3.0, Lambda Expressions
About


This comment originally written by:
NET 2.0 introduced Anonymous Methods . The idea behind anonymous methods it to write methods inline to the code. They are mainly used for small methods that don't require any need for reuse. For example, we have this code: 1: bool isGreaterThan5(
This comment originally written by:Maor David
Last week it was exactly one year since I started blogging, so this is my blog first birthday!! Come and read about the blog's statistics, top posts and more.
This comment originally written by:Maor David
Last week it was exactly one year since I started blogging, so this is my blog first birthday!! Come and read about the blog's statistics, top posts and more.
This comment originally written by:Maor David
Last week it was exactly one year since I started blogging, so this is my blog first birthday!! Come and read about the blog's statistics, top posts and more.
This comment originally written by:Maor David – The Blog
How many times have you written wrapper methods for objects that in reality you wished were part of the
This comment originally written by:Maor David’s Blog
Language Integrated Query (LINQ) introducing a standard set of operators that can be used to query several
This comment originally written by:Maor David-Pur
kolbis, intrersting point. In a nutshell,I wrote an application and fired up ILDASM and select the application.
The anonymous method and the lambda expression essentially compile to the same IL internally. Hence, their executions are similar.
Conclusion: the lambda expression is automatically converted into a corresponding delegate. The fact that it is no longer "strongly-typed", but the compiler is inferring the type.
This comment originally written by:Maor David-Pur
Kolbis asked: what goes on behind the lambda expression? How does the compiler translate it and to what?