Is it possible to have a float constant in my struct
private struct MyStruct
{
public const float[] TEST = new float[] {1,2};
}
The preceding was my first guess but doesn't work.
Any ideas?
No. But you could do this:
private struct MyStruct
{
public static readonly IList<float> TEST = Array.AsReadOnly(new float[] {1,2});
}
Not using Array.AsReadOnly means that people could not make TEST point to a different array, but the array you have assigned could have its contents changed.
No you cannot have a const float[]
Most commonly handled with some varient of
public static readonly float[] TEST = new float[] {1,2};
But that array isn't itself immutable, thus you often go along the lines of
public static readonly IList<float> TEST = new ReadOnlyCollectioN(new float[] {1,2});
Finally, the last option is to create your own immutable representation of a float[] that can be instantiated and provide the same actions as float[] without being modified.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With