Lambda Expression

Anatomy of the Lambda Expression

C# 3.0(.NET 3.5) introduced the lambda expression along with LINQ. The lambda expression is a shorter way of representing anonymous method using some special syntax.

For example, following anonymous method checks if student is teenager or not:

delegate(Student s) { return s.Age > 12 && s.Age < 20; };

The above anonymous method can be represented using a Lambda Expression in C# and VB.Net as below:

s => s.Age > 12 && s.Age < 20

Lambda Expression in VB.Net

Function(s) s.Age  > 12 And s.Age < 20

Let's see how the lambda expression evolved from the following anonymous method.

delegate(Student s) { return s.Age > 12 && s.Age < 20; };

The Lambda expression evolves from the anonymous method by first removing the delegate keyword and parameter type and adding a lambda operator =>.

Last updated