Thursday, January 24, 2008 11:14 PM Sean Feldman

Lambda Expressions

I was looking at anonymous delegates today in .NET 2.0 and thinking how much "syntactical noise" it has and how clean and delicate it is with Lambda expressions. Remember though how it used to be?

obj.SomeEvent += new EventHandler(HandlerMethod);
private void HandlerMethod(object sender, EventArgs e) {}
obj.SomeEvent += new EventHandler(delegate(object sender, EventArgs e) {  });
obj.SomeEvent += delegate(object sender, EventArgs e) { };
obj.SomeEvent += (sender, e) => Console.WriteLine("nice!");
Filed under:

Comments

# rascunho » Blog Archive » links for 2008-01-25

Friday, January 25, 2008 3:21 PM by rascunho » Blog Archive » links for 2008-01-25

Pingback from  rascunho  » Blog Archive   » links for 2008-01-25

# re: Lambda Expressions

Friday, February 22, 2008 5:59 PM by Josh Schwartzberg

This works too.... (assuming you don't need the params)

obj.SomEvent += delegate { Console.WriteLine("niceaswell!"); };

# re: Lambda Expressions

Thursday, February 28, 2008 2:12 AM by parasit

nice!