Lambda expressions and nullable types

Posted by Mathew on Stack Overflow See other posts from Stack Overflow or by Mathew
Published on 2010-05-31T03:36:37Z Indexed on 2010/05/31 3:42 UTC
Read the original article Hit count: 224

Filed under:
|
|

I have two samples of code. One works and returns the correct result, one throws a null reference exception. What's the difference? I know there's some magic happening with capturing variables for the lambda expression but I don't understand what's going on behind the scenes here.

            int? x = null;
            bool isXNull = !x.HasValue;

            // this works
            var result = from p in data.Program
                          where (isXNull)
                          select p;
            return result.Tolist();

            // this doesn't
            var result2 = from p in data.Program
                         where (!x.HasValue)
                         select p;
             return result2.ToList();

© Stack Overflow or respective owner

Related posts about c#

Related posts about LINQ