Deep C# - Anonymous Methods, Lambdas And Closures
Written by Mike James   
Monday, 19 September 2016
Article Index
Deep C# - Anonymous Methods, Lambdas And Closures
From Anonymous Methods to Lamdas
Expressions Lambda to Expression Trees
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#

 Buy Now From Amazon


 Chapter List

  1. Why C#?

    I Strong Typing & Type Safety
  2. Strong Typing
    Why Strong Typing
  3. Value & Reference
  4.    Extract Value And Reference
  5. Structs & Classes
    Structs & Classes 
  6. Inheritance
  7. Interfaces & Multiple Inheritance
    Extract Interface
  8. Controlling Inheritance
    II Casting & Generics
  9. Casting - The Escape From Strong Typing
    Extract Casting I ***NEW!
  10. Generics
  11. Advanced Generics
  12. Anonymous & Dynamic
    III Functions
  13. Delegates
  14. Multicast Delegates
  15. Anonymous Methods, Lambdas & Closures
    IV Async
  16. Threading, Tasks & Locking
  17. The Invoke Pattern
  18. Async Await
  19. The Parallel For
    V Data - LINQ, XML & Regular Expressions
  20. The LINQ Principle
  21. XML
  22. LINQ To XML
  23. Regular Expressions
    VI Unsafe & Interop
  24. Interop
  25. COM
  26. Custom Attributes
  27. Bit Manipulation
  28. Advanced Structs
  29. Pointers 

Extra Material





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.


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


  • 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:


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.




Last Updated ( Thursday, 22 September 2016 )