Trouble With Debugging Lambdas

You may have already noticed that it's harder to debug errors that occur within a Lambda expression.  For instance, if you have this Lambda expression:

var entries = collection.Where(i => i.EndDate.Value >= new DateTime(2008, 2, 1));

Notice the type that EndDate represents, naturally it's a date but it's also a nullable one, so it's Nullable<DateTime> or datetime?.  When Value is called, a value has to exist for EndDate; otherwise, an exception gets thrown because you can't call Value when the value is null (HasValue can be used to check for null).  But the issue with Lambda's is that they don't throw an exception immediately.

Rather they throw an exception whenever you do anything with the entries variable.  As soon as you may do "var entryCount = entries.Count();" an exception will be thrown, seemingly an issue with this line but ultimately pointing to the lambda.  Now, to debug, if its your method being called, the debugger steps into it.  Otherwise, for framework code, it doesn't (except possibly if you enabled CLR debugging which I didn't).

Published Wednesday, July 08, 2009 4:50 PM by bmains
Filed under: ,

Comments

No Comments