Object reference not set to an instance while not being null

c#

Solution

An `IEnumerable<Thing>` implies deferred execution.

In your first fragment `subset` and `things` are never enumerated.

In the second fragment, it is the call to `Count()` that enumerates the lists and only then it comes to light that one of the `a` is null in `a => a.SomeFlag`.

Problem

I'm getting some unexpected behavior in my process. I'm doing the following. ``` IEnumerable<Thing> things = ...; IEnumerable<Thing> subset = things.Where(a => a.SomeFlag); String info = "null: " + (subset == null); ``` The above works and info tells me that the object isn't null. So I wish to check the number of the elements in subset by this. ``` IEnumerable<Thing> things = ...; IEnumerable<Thing> subset = things.Where(a => a.SomeFlag); String info = "null: " + (subset == null); String count = subset.Count(); ``` Now I get an exception giving me the error message: Object reference not set to an instance of an object. What do I miss?!

Original source