Compiler Error: Invalid rank specifier: expected',' or ']' on Two Dimensional Array Initialization

c#, multidimensional-array

Solution

The C# compiler thinks you're trying to declare a jagged array, and doing so incorrectly. A jagged array is an array of arrays, where each array contained within the main array can have a different number of elements. A jagged array is declared as follows:

int[][] jaggedArray = new int[numElements][];

Which would create an array that could hold "`numElements`" arrays of integers within it.

You want to declare a multidimensional array, e.g.:

int[,] grid = new int[g.cols, g.rows];

Problem

I have an assignment for a class that is to be done in C#. Being a complete C# newbie, I did the project in Java first and I'm now trying to convert it to C#. I have the following function which results in the following compiler error. Error: Invalid rank specifier: expected',' or ']' on the following line: ``` int[][] grid=new int[g.cols][g.rows]; ``` Visual studio is underlining the g in g.rows ``` public int[][] getConvergenceCounts(MandelbrotGrid g){ int[][] grid=new int[g.cols][g.rows]; for(int x=0;x<g.cols;x++){ for(int y=0;y<g.rows;y++){ double tx=x*(double)3/400-1.5; double ty=y*(double)3/400-1.5; grid[x][y]=getConvergenceCount(new Complex(ty,tx)); } } return grid; } ``` I have no idea what I'm doing wrong here and reading up on Multidimensional arrays in C# didn't seem to help.

Original source