Search This Blog


Friday, July 10, 2009

Understanding Lambda Expression via Step By Step Debugging

Lambda Expression, anonymous function and the likes can be difficult to understand for C# programmers, partly because they don't work in a strict procedural manner, and partly because the abstract level one has to raise in order to grasp the concept is comparatively high. Lambda expression is the essence of functional programming, and the ability to understand and master functional programming will make you a better programmer, or deliver killer products at a rate so fast that your competitors won't even stand half a chance to play catch up with you... if you believe in Paul Graham.

No, this post is not about Lisp, or the superiority of another functional programming. This post is just a step by step step through over a simple lambda expression code, completed with screen captures, and debug values, so that hopefully at the end of this post, you will have a better understanding about what lambda expression is, and how it operates.

Let me start off with a code snippet:

public void RunTest2()
            DisplayValue((b, h) => b + h, 5.0, 10.0);
            DisplayValue((b, h) => b*h, 5.0, 10.0);
            DisplayValue((b, h) => b - h, 5.0, 10.0);
            DisplayValue((b, h) => b / h, 5.0, 10.0);

        private void DisplayValue<T, U>(Func<T , U, double> delegateReturnValue,
            T input1, U input2)

            Console.WriteLine("The calculated value is " + delegateReturnValue(input1, input2));

Basically this is a subroutine to get the following output:

The calculated value is 15
The calculated value is 50
The calculated value is -5
The calculated value is 0.5

If you don't understand what's going on, don't worry. Because I am going to show you how lambda expression works, in a step-by-step manner.

Launch your debugger, place a break point at the the first line:

Note that at this point, the b and h are not defined. There is really nothing to see at this stage.

Press F11, step into the DisplayValue method. Note that we are stepping into DisplayValue, the lambda expression we define above hasn't been executed, yet.

Note the function delegate delegateReturnValue, the T and U refer to the type of the two inputs, respectively, and the double is the return type. In this case, we dictate that the return type is a double. delegateReturnValue is a function handler, meaning that instead of storing a variable, or a pointer to a variable, it is storing a pointer to a method.

Think about this for a moment, the delegateReturnValue is a pointer to a method, which means that we can invoke the method at anytime we want inside the DisplayValue! So the break point, we can invoking the delegateReturnValue and pass in two input parameters, in this case, it's {input1=5.0, input2=10.0}.

Press F11 again, note that only now the lambda expression is invoked:

b, and h assume the input1 and input2 values. The delegateReturnValue is thus equivalent to the following function:

double PlusValue(double b, double h)
   return b+h;

But instead of declaring a function, we use lambda expression to reduce the amount of typing.

Press F11 again, this time you are back to the DisplayValue function, in order to display the calculation result.

So that's it! That's how you can see lambda expression in action!

No comments: