Lambda Expressions

C# 2.0 introduced anonymous methods, which are methods defined inside a
method. Incredibly powerful and a nice way to put all kinds of evaluation
logic inside your code they had the drawback that they could be quite hard
to read.

Func mySeniorStaffFilter = delegate(int a) { return a > 35; };

The above method takes an integer as a parameter, and returns a boolean. It
checks if the staff member passed to it is older than 35. If so, it returns
true.

Lamba expressions make things a little easier to read, while being
functionally exactly the same:

Func mySeniorStaffFilter = a => a > 35;

Even better, you can define them anywhere a delegate would have fitted:

var SeniorStaff = Employees.Where(s => s.Age > 35);
 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.