Incorrect possible null-ref warning?

Answered

Why does Resharper throw a possible NullReferenceException warning on the following code (on list.Contains):

List<string> list = GetList(); // which returns null

if ((list?.Count ?? 0) != 0)
{
    bool num = list.Contains("x");
}

 

The following code won't throw the warning. It looks the same to me:

List<string> list = GetList(); // which returns null

if (list != null && list.Count != 0)
{
     bool num = list.Contains("x");
}

1
2 comments
Official comment

Hello 

There is pretty similar ticket in YouTrack https://youtrack.jetbrains.com/issue/RSRP-460110. Feel free to comment/vote for it.

Thanks! 

I've followed your suggestion and voted the ticket, although the title in that ticket doesn't really describes what happens here. Seems more like the coalesce is not interpreted the right way.

1

Please sign in to leave a comment.