sfeldman.NET

.NET, code, personal thoughts

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!");
Published Thursday, January 24, 2008 11:14 PM by Sean Feldman
Filed under:

Comments

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

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

# re: Lambda Expressions@ Friday, February 22, 2008 5:59 PM

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

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

by Josh Schwartzberg

# re: Lambda Expressions@ Thursday, February 28, 2008 2:12 AM

nice!

by parasit