Float [] in the structure

Is it possible to have a float constant in my structure

private struct MyStruct
{
  public const float[] TEST = new float[] {1,2};
}

The previous one was my first guess, but it doesn't work.

Any ideas?

+3
source share
3 answers

No, you cannot have const float []

Most often some var is processed

public static readonly float[] TEST = new float[] {1,2}; 

But this array itself is not immutable, so you often go around the lines

public static readonly IList<float> TEST = new ReadOnlyCollectioN(new float[] {1,2}); 

Finally, the final option is to create your own immutable representation of float [], which can be created and provide the same actions as float [], without modification.

+1
source

No. But you can do this:

private struct MyStruct
{
  public static readonly IList<float> TEST = Array.AsReadOnly(new float[] {1,2});
}

Array.AsReadOnly , TEST , .

+4

Constant initializers must be compile-time constants

new float[] {1,2} 

creates a new object at run time, not compile time. However, you can make it a readonly static field, i.e.

public static readonly float[] TEST = new...
0
source

Source: https://habr.com/ru/post/1774611/


All Articles