- Home /
Best Practice for Vector Nulls
I've learned that the Vector classes do not support null assignments in C#. What have you found to be the best representation for Vector nulls? (Something analogous to float.NaN or float.MinValue for float null stand-ins)
Answer by Owen-Reynolds · May 13, 2014 at 02:33 PM
Since Best Practice sort of means "the way everyone else does it": traditionally, you'd set x (or all three) to something like -9999. It was commonly understood that if(pos.x>-9999)
was a "does this exist?" test.
Is this more efficient than setting it as a nullable type? (I'm trying to understand the reasoning behind each convention)
It looks like the nullable type just adds an extra "isThisNull" bool, so takes a little more space, then a little more time unsetting it during each assignment. But checking hasValue
is probably a tad faster than float compare A.x<999
.
Then nullables require(?) A.Value.x
ins$$anonymous$$d of just A.x
. Probably the same speed, but uglier.
Just looked at the last line of your Q again -- this is just the $$anonymous$$Value trick.
I see, thanks for taking the time to lay that all out for me. I haven't worked with Object Oriented languages in a while. Still trying to get a grasp of how it all pans out efficiency-wise.
Answer by Maerig · May 13, 2014 at 06:53 AM
If you really need it to be null, you could use Vector3
as a nullable type (`Vector3?`).
Your answer
