Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

float[] in a struct

Tags:

c#

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?

like image 550
Rod Avatar asked Mar 05 '26 08:03

Rod


2 Answers

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.

like image 184
cdhowie Avatar answered Mar 07 '26 22:03

cdhowie


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.

like image 40
Chris Baxter Avatar answered Mar 07 '26 22:03

Chris Baxter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!