Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tagged as

Inline functions are great!

, 12 Apr 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
A simple use for a new(ish) feature in .NET.

So, I've been getting to grips with using Lambda functions instead of creating simple methods that only include one line.

The main driver behind this is the fact that I'm trying to make my code readable (using StyleCop has helped with this, and I'm sure I'll blog about that too) so I can tell others what I expect.

This area is something that's always been alien to me, and I only knew they existed last year when the .NET guru at my previous company introduced me.

Now the code I've used it in previously looked like this...

var settings = new XmlReaderSettings 
{ 
   ValidationType = ValidationType.Schema, 
   Schemas = xss 
};
   settings.ValidationEventHandler +=
      new ValidationEventHandler(this.settings_ValidationEventHandler);

...

private void settings_ValidationEventHandler(object sender, 
           ValidationEventArgs e)
{
   this.validationErrors.Add(e);
}

This seems to be a pointless method in my opinion, the only advantage is a hypothetical benefit that I may want to reuse it. For the purpose of this code, I won't ever need that.

So I've changed it to this...

var settings = new XmlReaderSettings 
{ 
   ValidationType = ValidationType.Schema, 
   Schemas = xss 
};
   settings.ValidationEventHandler += 
         (s, e) => this.validationErrors.Add(e);

it's simple really.

To explain (in my own humble way that's probably wrong)...

if something is expecting a delegate function, starting it with "()" means that you're writing a function in line. If the function needs parameters, you give the parameters a name by adding them in the order they need to be (in this case, the first is an object, normally called "sender" when using autogenerated methods, the second is the args, normally called "e"). So you end up with (s, e).

Then you used the "=>" (pronounced "in to"), and then the action(s) you want to run.

So there you have it, a simple use for a new(ish) feature in .NET. Makes code reading simpler, reduces non-sense methods.

One downside in doing it this way is that you can't remove the handler without disposing of the object (settings) completely and starting again.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Martin Thwaites

United Kingdom United Kingdom
No Biography provided

Comments and Discussions

 
QuestionMore lambda Pinmemberlpll9615-Apr-12 0:26 
GeneralRemoving Event Handlers ? Pinmemberstooboo12-Apr-12 13:53 
GeneralRe: Removing Event Handlers ? Pinmemberstooboo12-Apr-12 13:54 
GeneralRe: Removing Event Handlers ? PinmemberMember 461529613-Apr-12 1:49 
GeneralRe: Removing Event Handlers ? PinmemberRichard Deeming17-Apr-12 9:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141223.1 | Last Updated 12 Apr 2012
Article Copyright 2012 by Martin Thwaites
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid