Olaf
Olaf

Reputation: 10247

Explanation for weird ReSharper behaviour?

Usually I rely on R# hints and suggestions, and often I let it auto-rewrite parts of my code. However, when I encounter issues like the one below I wonder if this is wise.

This code show no "possible null reference exception" warning:

enter image description here

If I add some code that checks Basket for null (no other changes anywhere), suddenly R# warns me:

enter image description here

This happens in an old ASP.NET website where "Basket" is a page scope object with a protected modifier (I don't know if that matters).

What is R# doing here? Is it inferring that just because the coder tests an object for null it actually might be null?

Upvotes: 0

Views: 45

Answers (1)

citizenmatt
citizenmatt

Reputation: 18603

By default, ReSharper is optimistic about values NOT being null. (You can change this in the options, so ReSharper will treat values as being null by default). But, as soon as you start checking for null, this optimistic assumption is broken, and ReSharper will start doing null checks.

Upvotes: 1

Related Questions