Dear community,
How do I pass a subarray from a multidimensional array as a parameter to a function?
In c the code would be like:
int array[5][5];
array[3][1]= 3;
dosomething(array[3]); // passes the 4th subarray to the function
Declared as:
void dosomething(int[]);
How will the port to c# look like?
int[][] array= new array[5][5]; // syntax error
int[][] array= new array[5,5]; // compile error: cannot cast [,] implicitely to [][]
int[,] array= new array[5,5];
array[3][1]= 3;
dosomething(array[3]); // compile error, 2 dimensions expected
Can someone help me out?
Thanks,
Carl
Loading
AlanPosted Jun 11, 2007, 10:00 AM
Anyway, the previous code using a multidimensional array would look like this:
int[,] array = new int[5,5]; // all values set to zero
array[3,1] = 3;
int[] array3 = new int[5]; // declare one dimensional array to get subarray in 4th row
for(int i = 0; i < 5; i++)
array3[i] = array[3, i];
dosomething(array3);
AlanPosted Jun 11, 2007, 8:46 AM
int[][] array= new int[5][];
for (int i = 0; i < 5; i++)
array[i] = new int[5];
array[3][1]= 3;
dosomething(array[3]);