Understanding Lambda Expression via Step By Step Debugging

No.of Views1376
Bookmarked0 times
Downloads 
Votes0
By  nsoonhui   On  07 May 2010 10:05:28
Tag : CSharp , How to
Lambda Expression, anonymous function and the likes can be difficult to understand for C# programmers
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

 

Introduction

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. 

Image Loading
 
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.
 
Image Loading

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:

 

Image Loading

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.

 

Image Loading

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

 
Sign Up to vote for this article
 
About Author
 
nsoonhui
Occupation-Not Provided
Company-Not Provided
Member Type-Fresh
Location-Malaysia
Joined date-06 May 2010
Home Page-Not Provided
Blog Page-Not Provided
 
 
Other popularSectionarticles
Comments
By:drfrDate Of Posted:6/14/2011 4:19:28 AM
not bad...
Thanks, now i am starting to understand the lambda expression.Wainting for this one...
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top