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



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");


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.



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


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.



Deep C# - Delegates

Delegates are C#'s original way of allowing you to work with functions as if they were first class objects. The aim may be simple but the need to define a type and then an instance of the type can be  [ ... ]

Deep C# - Passing Parameters

Passing parameters is easy as it always works in the same way, but the effects aren't always the same. It can be confusing and even error prone unless you understand how it all works. So does C# pass  [ ... ]

Other Articles


blog comments powered by Disqus







Last Updated ( Tuesday, 20 May 2014 )

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