|
Page 2 of 2
With the type defined we can now call the delegate in the usual way:
Hello2("Hello delegate 2");
The situation is a little more complicated than this simple example suggests.
In fact the anonymous method doesn’t have to match the signature of the delegate type exactly.
As long as the delegate type has no out parameters then the anonymous method can be defined with no parameters.
For example the following is perfectly legal even though the delegate type specifies a single string parameter:
MyHelloDelegateType2 Hello3 = delegate { MessageBox.Show("Default message!"); };
However you still have to call the delegate with the correct signature:
Hello3("dummy");
The parameter supplied is simply thrown away and you can see why this approach doesn’t work with out parameters. If there is an out parameter defined, where would the return value come from?
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 function 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.
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.
Getting started with C# Metro apps
How does Metro development in C# differ from desktop development? After looking at some general differences and the overall structure of a Metro app, we move on to consider how to make use of asynchro [ ... ]
|
How to number crunch - NAG for .NET
Number crunching in C# (or any .NET language) is a problem because it doesn't have a long tradition of implementing numerical methods. So why not use a library that was originally implemented in Fortr [ ... ]
| | Other Articles |
<ASIN:0470495995>
<ASIN:1430225254>
<ASIN:0596159838>
<ASIN:0672331012>
<ASIN:0470502266>
<ASIN:1430226536>
|