I’m trying to create a 2D array to store some values that don’t change like this.
const int[,] hiveIndices = new int[,] {
{200,362},{250,370},{213,410} ,
{400,330} , {380,282} , {437, 295} ,
{325, 405} , {379,413} ,{343,453} ,
{450,382},{510,395},{468,430} ,
{585,330} , {645,340} , {603,375}
};
But while compiling I get this error
hiveIndices is of type 'int[*,*]'.
A const field of a reference type other than string can only be initialized with null.
If I change const to static, it compiles. I don’t understand how adding the const quantifier should induce this behavior.
Actually you are trying to make the array – which is a reference type –
const– this would not affect mutability of its values at all (you still can mutate any value within the array) – making the arrayreadonlywould make it compile, but not have the desired effect either. Constant expressions have to be fully evaluated at compile time, hence the new operator is not allowed.You might be looking for
ReadOnlyCollection<T>For more see the corresponding Compiler Error CS0134: