Unity has a struct Color. It's default value is Color.white. So to check if it is its default value, I can use myColor == default(Color)
or myColor == Color.white
.
However, I came across some code that checks if myColor == null
. The code compiles, and Visual Studio gives the suggestion to turn it into false
. I assume this is because a Color cannot be null. This applies to all Unity structs I have seen, such as Vectors as well.
If I use a regular struct in C#, it gives me the suggestion, but also fails to compile.
Color myColor = new Color();
bool defaultColor = myColor == null; //Compiles
MyStruct myStruct = new MyStruct();
bool defaultStruct = myStruct == null; //Does not compile
Why do Unity Structs allow null checks? Why does this compile?