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#

Chapter List

Value And ReferenceValue and reference are a fundamental division in the way C# treats data. It is important that you understand the differences and most importantly when to use a struct and when to use a class. These aren't just differences in efficiency, they affect the semantics too.

Passing ParametersPassing 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.

InheritanceInheritance is a simple idea, until you try to make use of it.

Casting – the escape from strong typingCasting is one of the most confusing aspects of any modern language and it often makes beginners think hard. But if you know why you are doing it then the how makes a lot more sense.

Controlling InheritanceInheritance is a great idea but it is a powerful technique that can be misused. C# provides the tools to keep inheritance under control or to turn it off completely if you want to.

DelegatesDelegates 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 confusing. Let's see if we can make it all seem logical.

Multicast delegates and eventsMulticast 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, Lambdas And ClosuresAnonymous 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.

Take Exception To Everything

Dangerous PointersIn C# pointers have been replaced by references but there is still a need for the raw dangerous original concept. You could say "what's so dangerous about pointers?"

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:

Create the delegate type which specifies the signature and return type.

Write the method that you want the delegate to "wrap".

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: