[818] Possible NRE

In the code below I get a "possible NRE" over the last reference to "predicate". Am I missing something or is the warning wrong?

Thanks

Sean

PS: Is this new forum software? Very nice if it is!

public IEnumerable Where(IEnumerable items, Predicate]]> predicate)
{
foreach (T item in items)
{
if (predicate == null)
yield return item;

if (predicate(item))
yield return item;
}
}

2 comments
Comment actions Permalink

yield return doesn't stop the flow, it will continue after a MoveNext() is called again, so when predicate is null it will also call the if (predicate(item)) and you WILL get an NRE.

public IEnumerable Where(IEnumerable items, Predicate predicate) { if (predicate == null) predicate = x =>]]> true;

foreach (T item in items)
{
if (predicate(item))
yield return item;
}
}

0
Comment actions Permalink

Thanks XIU, that makes sense.

Cheers

Sean

0

Please sign in to leave a comment.