My Software Notes

Useful things I discover

Closures in C# (better known as lambdas)

with 2 comments

I’m a big fan of Martin Fowler, the author of such brilliant and practical books as Refactoring: Improving the Design of Existing Code, Patterns of Enterprise Application Architecture and UML Distilled (these are books that every developer should have, study and refer to often). Today I was reading an article on his web site and I came across the term “Closure“. I’d come across it some years ago whilst reading another article on his site and I’d gotten a basic concept of what it meant at the time, but I never really used it, so the concept became foggy.

Today when looking over his description of what a closure is I realized the obvious: for us .NET types a closure can be accomplished with a lambda expression.

I read the full article on Martin’s site then clicked on the “C#” link at the bottom and was taken to the old blog of Joe Walnes, where he gives C# 2.0 versions of Martin’s closure examples.

I looked at them and thought how would you do these in C# 3.0? Well, I couldn’t resist the challenge, so here they are.

The main difference now between the Ruby examples and the C# examples is the type declarations in C#: you have to say what the methods return, what types their parameters are and that threshold is an int. But, notice that with the lambda expressions you don’t have to specify the type because it is inferred by the compiler.

While I was doing this I got a bit carried away and created more ways of implementing these, but to keep the emphasis on the comparison between the original Ruby closures and the .NET lambdas I will put the other versions in another post: Fun with lambda expressions.

def managers(emps)
  return {|e| e.isManager}
def highPaid(emps)
  threshold = 150
  return {|e| e.salary > threshold}
def paidMore(amount)
  return {|e| e.salary > amount}
highPaid = paidMore(150)
john =
john.salary = 200


  C# 3.0
public List<Employee> Managers(List<Employee> emps) {
    return emps.FindAll(e => e.IsManager);
public List<Employee> HighPaid(List<Employee> emps) {
    int threshold = 150;
    return emps.FindAll(e => e.Salary > threshold);
public Predicate<Employee> PaidMore(int amount) {
    return e => e.Salary > amount;
var highPaid = PaidMore(150);
var john = new Employee();
john.Salary = 200;

Written by gsdwriter

June 6, 2009 at 2:04 pm

Posted in Languages

Tagged with , ,

2 Responses

Subscribe to comments with RSS.

  1. […] While creating C# 3.0 equivalents to the Martin Fowler example Closures in Ruby in my last post (Closures in C# (better known as lambdas)), I was having so much fun that I carried on and came up with […]

  2. thanks for your sharing…


    June 15, 2010 at 3:39 am

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: