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);
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);

Comments