SingingEels : Development Community & Resource

Login

Articles

  • ADO.NET (2)
  • ASP.NET (36)
  • Azure (0)
  • LINQ (5)
  • Security (2)
  • Silverlight (3)
  • SQL (7)
  • Standards (5)
  • WCF (2)

Syndication

  • Articles RSS
  • Blogs RSS

Contribute

  • Our Authors List
  • Member Sign-Up
  • Suggestions Box

Lambda Expressions to Replace Inline Delegates

(Oct 07 2007 - 07:40:31 PM by Timothy Khouri) - [print blog post]

With .NET 2.0 came a very handy feature of "inline functions" using a very simple 'delegate + method-body' syntax. I quickly adopted this for those quick "one-liners" that seemed pointless to make an actual method for. Example:

this.CancelButton.Click += delegate
{
   this.Response.Redirect("~/View.aspx", true);
};

A function that is basically one line of code, in my opinion, really doesn't need to have a full method created for it. The 'old' way of doing the above would have been something like this:

// This line would be in the OnInit method or something.
this.CancelButton.Click += new EventHandler(this.Cancel_Click);

// And this method is a bit overkill.

private void Cancel_Click(object sender, EventArgs e)
{
   this.Response.Redirect("~/View.aspx", true);
}

Lambda Expressions Replace Inline Delegates

As I pointed out in the title of this blog post, I'm no longer going to be using inline delegates for basically anything. I didn't realize I could do this with lambda expressions until a collegue pointed it out to me, but here is how to accomplish the above using lambda in C#.

this.CancelButton.Click += (sender, args) => this.Response.Redirect("~/View.aspx", true);

It may not look like we've done much, but this is really beautiful! First of all, we are able to utilize the parameters if we wanted to, and secondly we are able to drop the "method body" sytax by removing the { and } characters because this is a one line method.

If you wanted to have multiple lines of code executed, you could do something like this:

this.MyButton.Click += (sender, args)
{
   // any amount of code here... you can also use

   // the "sender" and "args" objects as you'd like.

};

Another great benefit of using the lambda expressions in this way is that you can re-use the same variable names because the are "scope specific". Example:

this.MyButton.Click += (sender, args) => DoSomething();

this.MyOtherButton.Click += (sender, args) => DoSomethingElse();

Enjoy!

  • Dec 11 2007 - 11:13:17 AM davidkiff

    Nearly! One small => missing!

    this.MyButton.Click += (sender, args) => //HERE

    {

    // any amount of code here... you can also use

    // the "sender" and "args" objects as you'd like.

    };

You must be logged in to add comments. If you have not already done so, you can create an account here. If you already are a member, you first need to login before you can comment.

Developer / Architect / Author

Blog Archives

  • August 2010 - (1)
  • June 2009 - (1)
  • January 2009 - (1)
  • November 2008 - (1)
  • October 2008 - (2)
  • September 2008 - (2)
  • August 2008 - (3)
  • July 2008 - (1)
  • June 2008 - (3)
  • May 2008 - (2)
  • April 2008 - (2)
  • March 2008 - (4)
  • February 2008 - (2)
  • December 2007 - (2)
  • November 2007 - (1)
  • October 2007 - (4)
  • September 2007 - (9)
  • August 2007 - (7)

Related Ads

SingingEels.com as of Feb 04 2012 - 12:05:13 AM - (0.0468759)