-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unexpected Dereference of a possibly null reference warning #38084
Comments
Change |
Thanks, I assumed the compiler would remember that the customer can't bee null anymore due to the initial if-statement and that the use of ? wouldn't affect that. |
I believe this is really a manifestation of #37394, which is fixed in 16.4. I tried it out just now and it's handling this scenario as expected (no diagnostics even when using Thanks for reporting this issue. |
Basically, it's true that conditional-accessing something with |
Shouldn't be a warning, just an info in the line |
You could always write an analyzer to check for those sorts of redundancies :) |
Having the same warning when using |
Actually, an XmlNodeList can be empty; and unlike other lists, XmlNodeList.Item(int) does not throw an IndexOutOfRangeException but simply returns null when the index is too big. (See https://docs.microsoft.com/en-us/dotnet/api/system.xml.xmlnodelist.item?view=net-5.0) Or did you mean, |
Version Used:
Visual Studio 2019 16.2.2
.NET Core 3 , Preview 8
Steps to Reproduce:
When I compile this code, I get a Dereference of a possibly null reference warning in the Console.WriteLine statement.
See the code here:
https://sharplab.io/#v2:EYLgtghgzgLgpgJwDQBMQGoA+ABATARgFgAoEgYgDsBXAGxomBrgAI4KGmTsBmZvZgCpxYJAN4kSzKX17YALMwAiAewDKMKgDNNACgDCVWMrCIA/MwDGhmMcQBKSdPHFpr5gEtNzHVaMmEzACEALzM1HQOLm5SztHRnt6+Nv6mAHQAchAmaXrKFDAQ7hRQOgBEABZwdMqldsym5poQNFBwkXFusR1x2PgAnD7WtggZWW0A3I7dzAC+U25zUVKLi1yyuMwGfohi8zx8+AAMzJkmzKGlABJVNDWTxDNAA=
Expected Behavior:
I would expect no warnings from the code, because the customer can never be null when the Console WriteLine is called.
Actual Behavior:
I get a warning "warning CS8602: Dereference of a possibly null reference."
The text was updated successfully, but these errors were encountered: