Lambda Expressions

In C# 2.0 there are anonymous methods, which allows you to write your method code inline instead of creating a new method in your class. In C# 3.0 there is a new feature – lambda expressions. The goal of this expressions is the same as the anonymous methods, but the syntax is more concise.

Let’s have the following code:

If we want to rewrite this code using anonymous method, it will get the following look:

And if we want to rewrite this code using lambda expressions, it will look like this:

Isn’t it so simple? 🙂 The sign ‘=>‘ is read as “goes to”. We first indicate the arguments of our method and after that we do our calculations in it.