Yippie-Ki-Yay
Yippie-Ki-Yay

Reputation: 22854

C# linq null question

Could someone explain me how can this be possible:

foreach (var pair in Expected.Zip(
         Actual, (x, y) => new { Expected = x, Actual = y }))
{
    // No match for a 'null' series.
    if (pair.Actual == null) yield return 0;

    var actualPaths = pair.Actual.Images.Select(x => x.Path).ToList();
}

This code (in Microsoft Visual Studio 2008) stops on line var actualPaths = ... and says that pair.Actual equals null, therefore raising a NullReferenceException.

How is this even possible? Am I missing something?

Upvotes: 2

Views: 322

Answers (1)

SLaks
SLaks

Reputation: 888243

After your if, the rest of the code keeps running.

You need to add continue;, or put the rest of the code in an else block.

Upvotes: 8

Related Questions