简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM