How do I create a constant element for an immutable type?
If you have an immutable type:
struct Point3
{
}
and a member inside of the same origin:
public static const Point3 Origin = new Point3 (0,0,0);
should use:
new Point3 (0,0,0)
?
It seems to me that since the type cannot be changed, why do so many origins have essentially the same thing? How we never change 0, right?
How do you achieve the same for immutable types?
a source to share
As Andrew mentioned, you cannot use const
for this, because it is not a compile time constant.
Note that if you are going to use the constructor multiple times, you would be better off (from a performance standpoint) to call
new Point3()
than
new Point3(0, 0, 0)
The compiler knows that the first version will just run out of memory and does not need to call any code.
However, I would go along with providing the member Origin
and use it everywhere if possible :)
a source to share