Anonymous Methods In C#
Written by Mike James   
Tuesday, 01 April 2014
Article Index
Anonymous Methods In C#
Using anonymous methods

Banner

 

Using anonymous methods

So what are anonymous methods good for?

They certainly save one level of naming confusion but in some cases they can remove the need for any new names at all.

For example, consider the Find method of the Array object defined as:

public static T Find<T> (T[] array,
                          Predicate<T> match)

The Predicate delegate is defined as:

public delegate bool Predicate<T> (T obj)

Without anonymous methods you would have to define a Predicate method, wrap in a delegate and pass it to the Find method.

With anonymous method it can be as simple as:

int result = Array.Find(A,
        delegate(int x) {return (x < 0);});

In short, anonymous methods are good for short functions that you want to use “at once”, often as a parameter within a method call.

Lambda expressions

In version 3.0 of .NET anonymous methods were augmented by lambda expressions - and this often causes confusion about which facility does what.

A lambda expression can be considered an even lighter weight anonymous method.

You still need to define a delegate type and this determines the number and type of the parameters used in the lambda expression. The basic idea is that the method is simply assigned to the delegate instance using a special notation:

(parameters)=> method body

Apart from this change a lambda expression can be treated exactly like an anonymous method.

For example, our previous "Hello" example can be written:

MyHelloDelegateType Hello=() => MessageBox.Show(
                         "Hello From a Delegate");

Hello();

In the same way the Array.Find example can be written:

int result = Array.Find(A,x=> return (x < 0));

Notice that in this case, as there is only a single parameter, x, we can also follow the convention of dropping the brackets making it look even simpler.

The bottom line is that lambda expressions are just a shorthand way of creating anonymous methods.

However this doesn't mean that they aren't worth using or that they don't have any interesting subtleties that deserve further exploration.

csharp

Closure

Both anonymous methods and lambda expressions bring with them an interesting additional facility.

As both are declared within the context and scope of an enclosing method there is the possibility of defining how the enclosing and enclosed method can interact - and this brings us to the fascinating and useful topic of closure, the topic of another article.

 

Related Articles

Lambda Expressions

Multicast delegates and events

Closure

Introduction to Delegates

 

To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter, FacebookGoogle+ or Linkedin,  or sign up for our weekly newsletter.

 

blog comments powered by Disqus

 

Banner


.NET Regular Expressions in depth

If you think regular expressions are trivial and boring you've not seen the whole picture. Here we reveal that in .NET they are amazing powerful and not to be missed.



Not so complex numbers in C#

Did you know that .NET supports complex numbers? The Complex struct is easy to use and just needs a little extra publicity so that we all remember it's there!


Other Articles

<ASIN:0672336901>

<ASIN:1494208393>

<ASIN:1118833031>

<ASIN:1430242787>

<ASIN:1449380344>

<ASIN:1449320414>



Last Updated ( Saturday, 05 April 2014 )
 
 

   
RSS feed of all content
I Programmer - full contents
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.