In MonoDevelop I have the following code which compiles:
int[] row = new int[indices.Count]{};
However, at run-time, I get:
Matrix.cs(53,53): Error CS0150: A
constant value is expected (CS0150)
(testMatrix)
I know what this error means and forces me to then resize the array:
int[] row = new int[indices.Count]{};
Array.Resize(ref row, rowWidth);
Is this something I just have to deal with because I am using MonoDevelop on Linux? I was certain that under .Net 3.5 I was able to initialize an array with a variable containing the width of the array. Can anyone confirm that this is isolated? If so, I can report the bug to bugzilla.
You can’t mix array creation syntax with object initialization syntax. Remove the
{ }.When you write:
You are creating a new array of size
indices.Countinitialized to default values.When you write:
You are creating an array and then initializing it’s content to the values [1,2,3,4]. The size of the array is inferred from the number of elements. It’s shorthand for:
The array is still first initialized to defaults, this syntax just provides a shorthand to avoid havind to write those extra assignments yourself.