Is there any difference in terms of performance between
if (variable == null) ...
and
if (variable case null) ...
How compiler handles this?
Does case
work more like const?
Also, what about case
vs is
?
Is there any difference in terms of performance between
if (variable == null) ...
and
if (variable case null) ...
How compiler handles this?
Does case
work more like const?
Also, what about case
vs is
?
2
Answers
The performance difference between these two methods is almost non-existent. You should use the one that makes your code more readable.
if (variable == null)
should be identical toif (variable case null)
. Why would you expect the compiler to treat them differently? I would expect that it would be fairly trivial for the compiler to treat them the same way unless there’s evidence to the contrary.If you’re in doubt, you can use godbolt.org to compare the compiled output. Given:
versus
I get the same disassembly from both.