Deep C# - Anonymous Methods, Lambdas And Closures
Monday, 19 September 2016
Article Index
Deep C# - Anonymous Methods, Lambdas And Closures
From Anonymous Methods to Lamdas
Expressions Lambda to Expression Trees
Closure
Two Patterns for Closure

Anonymous methods aren't particularly new, but they have hidden depths and lead on to lambdas and the idea of a closure. These are all important ideas in modern programming.

This is a chapter of our ebook on C#, a work in progress.

Deep C#

 front

 Chapter List

  1. Value And Reference
  2. Dynamic C#
  3. Passing Parameters
  4. Inheritance ***NEW
  5. Casting – the escape from strong typing
  6. Controlling Inheritance
  7. Delegates
  8. Multicast delegates and events
  9. Anonymous Methods, Lambdas And Closures
  10. Take Exception To Everything
  11. What's The Matter With Pointers?
  12. Generics
  13. Structs
  14. The LINQ Principle
  15. Regular Expressions in depth
  16. Bit Manipulation
  17. Async
  18. The Parallel For
Multicast delegates and events
Tuesday, 25 May 2010
Article Index
Multicast delegates and events
Events
Generic Events

Multicast delegates are useful in their own right but they also form the basis on which the C# event system is built. We take a close look at how they work and how to use them. For example, did you know you could add and subtract delegates?

 

 

Anonymous methods were introduced back in .NET 2.0 and while they sound like something designed to implement “dirty” shortcuts they are a welcome addition to C#. In modern C# they have been superseded by the lambda expressions but there is still one thing that an anonymous method can do that a lambda can't so they are still worth knowing about. In addition statement lambdas are just a slightly different syntax for anonymous methods so they are the foundations upon which lambdas are built.

The big problem with anonymous methods is figuring out what the problem is that they are designed to solve.

So let’s take a look at what they are for.

On one level anonymous methods are just about making delegates easier to create. Let's go through the stages of creating a delegate.

Delegates

A delegate is just an object that “wraps” a function.

It is what you need when functions are not in themselves first class objects. That is you can't pass a function as argument to another function so you have to wrap it in an object so that you can pass the object and a delegate is just an object designed for wrapping a function.

Before you can use a delegate to wrap a function you have to create a delegate type that has the signature of the function you want to wrap. That is every delegate wrapper carries information about the function it wraps in its type - this is used to detect simple errors. 

Then you instantiate the type, wrap the function and use it.

So the steps to create and use a delegate are:

  1. Create the delegate type which specifies the signature and return type.
  2. Write the method that you want the delegate to "wrap".
  3. Instantiate the delegate type created in step 1 and use it to wrap the method.

What all this means is that you have to invent multiple names for what in most cases is a single idea -

  • the delegate type - MyDelegateType
  • the method to be wrapped - MyMethod

and

  • the instance doing the wrapping - MyDelegate

For example, if you want to wrap a “Hello World” function you first create a suitable delegate type:

delegate void MyHelloDelegateType();

then you have to create the function within some class or other:

void Hello(){
 MessageBox.Show("Hello From a Delegate");
}

and finally create an instance of the delegate type specifying the function that it is to wrap:

MyHelloDelegateType MyHelloDelegate1 =
             new MyHelloDelegateType (Hello);

or equivalently:

MyHelloDelegateType MyHelloDelegate1 = Hello;

Calling the function via the delegate is just a matter of using its name:

MyHelloDelegate1();

You can see that we have had to invent three names:

  • MyHelloDelegateType - the delegate type
  • Hello - the method name
  • MyHelloDelegate - the wrapper instance

This is fine if you are going to create multiple instances of the type and wrap multiple functions but in most cases the type, the delegate and the method are more or less a single entity.

That is you create a delegate because you want to use a method as if it was an object and often this is a one-off requirement.

csharp

 

 



Last Updated ( Thursday, 22 September 2016 )