Lambda functions are a way of constructing nameless(anonymous) functions on the fly. They’re very handy when you need a one off function or when you need to wrap a function. C# is a very friendly language for talking about lambda functions because it has a strong set of tools for working with them.

Here’s an example:

() => return 1;

And this is equivalent to this:

int someMethod(){

return 1;

}

Here’s another that takes two parameters, a and b:

( int a, int b ) => {

int c = a + b;

return c;

}

Notice that multi-line body of the function is surrounded by squiggly brackets; however, if the function is only one line, it does not require brackets.

And it’s equivalent to this:

int someMethod( int a, int b ){

int c = a + b;

return c;

}

The most powerful tool in C# for working with functions as data is the Delegate. It is a type declaration that allows what are essentially strongly typed function pointers to be stored and manipulated.

For example, we have the function add( int a, int b ) which adds a and b, then returns the result. If you just want to pass the function around so that other code can use it, we must declare the delegate type so that all necessary code can see it, then we can use it just like any other type.

// The delegate declaration public delegate int addFunc( int a, int b );

This declares a type ‘addFunc’ that holds a function that takes two integer parameters and returns an integer. Notice that this is a declaration of a type and not an instantiation of that type. It is very similar to a class in that it only describes a new type.

To use this delegate we can create a function and store it as so:

// The delegate declaration public delegate int addFunc( int a, int b ); int add( int a, int b ){

return a + b;

} addFunc myFunction = new addFunc( add );

We can call the stored function as so:

myFunction( 1, 2 ); // returns 3

Note that, at any time, we can change the function stored in myFunction as long as that new function matches the delegate signature. The best part is that client code won’t have to change to accommodate.

Let’s add lambda functions to the mix. Here’s the same code as before but with a lambda function rather than the regular function declaration:

public delegate int addFunc( int a, int b ); addFunc myFunction = new addFunc(( int a, int b ) => return a + b;) myFunction( 1, 2 ); // returns 3

Both behave the same way for client code calling myFunction.

Delegates can also be used as parameters for functions, like so:

public delegate int addFunc( int a, int b ); void myMethod( addFunc callback ){

callback( 1 , 2 );

} myMethod( ( int a, int b ) => return a + b; ); // returns 3

myMethod( ( int a, int b ) => return a - b; ); // returns -2

Now for the fun stuff.

Imagine you need to store (or pass) a function but you also need to store some contextual information for that function like a ‘this pointer’ or some value from another function. We can use lambda functions to bundle in some extra information, similiar to Javascript’s bind.

To do this, we can use a lambda function to wrap another function call up with some extra information. In this example, we’d like to use our add function to add 30 to any number passed in. The delegate is declared so that it only accepts one parameter, so this puts us in a bind. To resolve this, we can use a lambda function as so:

// The delegate declaration

public delegate addThirty( int a ); int add( int a, int b ){

return a + b;

} addThirty myFunction = new addThiry(

( int a ) => return add( a, 30 );

); myFunction( 1 ); // returns 31

A bit of a contrived example but it shows that we can use lambdas to abuse the type system as well as include extra information to avoid awkward context passing in systems.

I hope you found this informative. I can be reached at @DFXLuna on Twitter, a thread on reddit is available here.