Click here to Skip to main content
15,885,116 members
Articles / Programming Languages / C#

Inline Functions Are Great!

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
12 Apr 2012CPOL1 min read 11.5K   4   5
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...

C#
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...

C#
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)


Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionMore lambda Pin
lpll9614-Apr-12 23:26
lpll9614-Apr-12 23:26 
GeneralRemoving Event Handlers ? Pin
stooboo12-Apr-12 12:53
stooboo12-Apr-12 12:53 
GeneralRe: Removing Event Handlers ? Pin
stooboo12-Apr-12 12:54
stooboo12-Apr-12 12:54 
DOH ! - Sorry - Just read the last line of your post Wink | ;-) - please just ignore me !
GeneralRe: Removing Event Handlers ? Pin
User 461199613-Apr-12 0:49
User 461199613-Apr-12 0:49 
GeneralRe: Removing Event Handlers ? Pin
Richard Deeming17-Apr-12 8:31
mveRichard Deeming17-Apr-12 8:31 

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

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