Is there any reason why comparing struct types with null does not result in compile error?
When comparing struct types with null, it is always false. So, why doesn't the compiler throw compilation error?
When and where do we want to compare struct types with null?! never because it is a logical bug
10 Replies
Because afaik
==
is by default comparing an object to an object, so it boxes the struct and then compares it to null
.
And yes you're right, == null
for a struct will never return true.Thanks
my wrong
It says
So, it is compile time error
Turn on warning level 6 and it should flag it.
If
==
has been overloaded, it says it returns false always. OK, prefectI'm fixing like a zillion places in my codebase where we compare DateTime to null right as we speak, because we increased the warning level and now that's being flagged all over the place.
Because my class Node was class
but now it is struct, I should check null conditions in all places 🙂 and replace it
What do you mean?
Also, I do not know about warning flags. My env is Unity + Rider
I don't know what you mean by latest.
AnalysisLevel is something different
I'm talking about this:
<WarningLevel>6</WarningLevel>
Good question.
I don't know.
I only actually found out about level 6 because our official build system somehow enabled that recently and it started flagging a bunch of issues that did not reproduce when building locally. So I cranked up the warning level to 6 for local builds and now I've been spending hours fixing flagged issues.
If you want the maximum possible analysis level, set it to "preview"
I'm not sure what "latest" includes.
This is the documentation page, yes
So, why doesn't the compiler throw compilation error?This is what we called a load-bearing bug The native compiler didn't error for this code. When we created roslyn, we tried to fix it Then we found we couldn't compile VS or Office It, and the other warning wave warnings on that page, are the native compiler bugs that are so pervasive we had to make an entire new feature to fix them