Right, it's a struct, nevermind
And?
I'm using C# only so this will be .NET-specific.
My proposal was to avoid creating multiple Vectors with the same (zero) value. Why give the garbage collector pressure if you can avoid it?
But that's with classes, and Vector is actually a struct, so it's always instantiated anew on the stack. Here is the illustration of this issue:
class TestClass
{
public int x;
private static readonly TestClass _Zero = new TestClass { x = 0 };
public static TestClass Zero { get { return _Zero; } }
}
struct TestStruct
{
public int x;
private static readonly TestStruct _Zero = new TestStruct { x = 0 };
public static TestStruct Zero { get { return _Zero; } }
}
public static void Main()
{
unsafe
{
TestClass c0a = TestClass.Zero;
TestClass c0b = TestClass.Zero;
TestClass c1 = new TestClass {x = 0};
TestClass c2 = new TestClass {x = 0};
Console.WriteLine("c1 == c2 ? {0}", c1 == c2);
Console.WriteLine("c0a == c0b ? {0}", c0a == c0b);
Console.WriteLine("c1 == TestClass.Zero ? {0}", c1 == TestClass.Zero);
Console.WriteLine("c0a == TestClass.Zero ? {0}", c0a == TestClass.Zero);
TestStruct s0a = TestStruct.Zero;
TestStruct s0b = TestStruct.Zero;
TestStruct s1 = new TestStruct {x = 0};
TestStruct s2 = new TestStruct {x = 0};
Console.WriteLine("s1 == s2 ? {0}", &s1 == &s2);
Console.WriteLine("s0a == s0b ? {0}", &s0a == &s0b);
Console.WriteLine("s0a == s1 ? {0}", &s0a == &s1);
}
}
The output is:
c1 == c2 ? False
c0a == c0b ? True
c1 == TestClass.Zero ? False
c0a == TestClass.Zero ? True
s1 == s2 ? False
s0a == s0b ? False
s0a == s1 ? False
You can see that both TestClass.Zero variables are equal (reference to the same static Zero member), no object creation happening there. But with structs it's different: they are always instantiated, hence they have a different address.