The Artima Developer Community
Sponsored Link

Agile Buzz Forum
The power of closures in C# 2.0

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Joe Walnes

Posts: 151
Nickname: jwalnes1
Registered: Aug, 2003

Joe Walnes, "The Developers' Coach" from ThoughtWorks
The power of closures in C# 2.0 Posted: Sep 16, 2004 4:10 PM
Reply to this message Reply

This post originated from an RSS feed registered with Agile Buzz by Joe Walnes.
Original Post: The power of closures in C# 2.0
Feed Title: Joe's New Jelly
Feed URL: http://joe.truemesh.com/blog/index.rdf
Feed Description: The musings of a ThoughtWorker obsessed with Agile, XP, maintainability, Java, .NET, Ruby and OpenSource. Mmm'kay?
Latest Agile Buzz Posts
Latest Agile Buzz Posts by Joe Walnes
Latest Posts From Joe's New Jelly

Advertisement

Martin Fowler (obligitary Fowlbot namedrop) recently blogged about the power of closures in languages that support them. It's worth remembering that C# 2.0 has true closure support in the form of anonymous delegates. This includes reading and modifying variables outside the context of the closure - unlike Java's anonymous inner classes.

Just for kicks, I've rewritten all of the examples Martin's Ruby examples in C# 2.0. This makes use of the improved APIs in .NET 2.0 pointed out by Zohar.

Ruby C# 2.0
def managers(emps)
  return emps.select {|e| e.isManager}
end
public List<Employee> Managers(List<Employee> emps) {
  return emps.FindAll(delegate(Employee e) { 
    return e.IsManager; 
  }
}
def highPaid(emps)
  threshold = 150
  return emps.select {|e| e.salary > threshold}
end
public List<Employee> HighPaid(List<Employee> emps) {
  int threshold = 150;
  return emps.FindAll(delegate(Employee e) { 
    return e.Salary > threshold; 
  });
}
def paidMore(amount)
  return Proc.new {|e| e.salary > amount}
end
public Predicate<Employee> PaidMore(int amount) {
  return delegate(Employee e) { 
    return e.Salary > amount; 
  }
}
highPaid = paidMore(150)
john = Employee.new
john.salary = 200
print highPaid.call(john)
Predicate<Employee> highPaid = PaidMore(150);
Employee john = new Employee();
john.Salary = 200;
Console.WriteLine(highPaid(john));

The code difference between the languages isn't that difference. The C# 2.0 code is obviously longer (though not a lot) because:

  • C# 2.0 is staticly typed (let's not get started on the static vs dynamic debate).
  • C# 2.0 requires the 'delegate' keyword.
  • Ruby allows you to ignore the 'return' keyword.

You can try this stuff out yourself by playing with Visual C# Express.

Read: The power of closures in C# 2.0

Topic: Re: A Couple of SmallTalk Items Previous Topic   Next Topic Topic: How To Create A Custom Widget - Interior Decoration

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use