Debugging Lambda Expressions – Visual Studio 2015 has added the ability for developers to debug Lambda Expressions. This is a big improvement for me personally because I quite enjoy using Lambda Expressions. Now I can debug these Lambda Expressions and modify values to test different scenarios. Let’s take a look how we do it.
Debugging Lambda Expressions
To illustrate how easy it is to debug Lambda Expressions, I have created a simple method that reads order details into a list. As you can see this method returns nothing, but it is just to illustrate the use of Lambda Debugging. I have a Lambda Expression that returns only order items above a certain order value (OrderThreshold of 70). This is useful in a real world application where certain additional requirements are to be set for customers that make orders above a certain value.
From the returned list of orders we can now manipulate the expression in the Watch window. My threshold is 70, but let’s see if there are any orders above a higher threshold of 100 for example. We can now type the modified Lambda Expression directly into the Watch window. As you can see, the results are displayed nicely.
Developers can now also do the same in the Immediate Window. Below we are returning the total value of all the orders contained in the order items list.
Another nice tidbit is the ability to do the same thing from the pinned data tips. If you hover your cursor over the orderDetails variable, a tip will be displayed that shows you the returned items are 5.
Clicking the push pin will now keep that data tip pinned and always available during your debugging session.
If you right click this data tip, you can add a Lambda Expression there too.
Here I want to get the total value of all the orders (same as we did in the Immediate Window). I can type the Lambda Expression and hit enter.
The Expression is evaluated and the result is displayed in the data tip. This is a convenient solution for any developer several thousand lines deep in code. You are now free to explore, modify and correct faulty Lambda Expressions.
While the example of the above order items might not have contained big values or contained many orders, consider the usefulness of this Lambda Debugging feature on results containing a couple of hundred records with order values ranging from a couple of thousand to a few million. The ability to debug Lambda Expressions cuts through all this data and allows developers to fine tune the expression to return the exact result they need.
For extra reading, see Support for debugging lambda expressions with Visual Studio 2015 by Patrick Nelson.