C
C#•2y ago
Gladiator

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
Thinker
Thinker•2y ago
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.
Gladiator
GladiatorOP•2y ago
Thanks my wrong It says
Cannot apply operator '==' to operands of type 'Bitzooma.MiniCity.Utility.PathFinding.Node' and 'null
Cannot apply operator '==' to operands of type 'Bitzooma.MiniCity.Utility.PathFinding.Node' and 'null
So, it is compile time error
mtreit
mtreit•2y ago
Turn on warning level 6 and it should flag it.
Gladiator
GladiatorOP•2y ago
If == has been overloaded, it says it returns false always. OK, prefect
mtreit
mtreit•2y ago
I'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.
Gladiator
GladiatorOP•2y ago
Because my class Node was class but now it is struct, I should check null conditions in all places 🙂 and replace it
mtreit
mtreit•2y ago
What do you mean?
Gladiator
GladiatorOP•2y ago
Also, I do not know about warning flags. My env is Unity + Rider
mtreit
mtreit•2y ago
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.
333fred
333fred•2y ago
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
Want results from more Discord servers?
Add your server