Click here to Skip to main content
15,905,558 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
What Is The Difference Between int [,] And int [] [] And I Also Want To Know That Why It Throws A Error By Going Through Syntax Mention Below

int[,] a = new int[5, 2] { { 0, 0 }, { 1, 2 }, { 2, 4 }, { 3, 6 }, { 4, 8 } }; // work fine

int[] [] b = new int[5, 2] { { 0, 0 }, { 1, 2 }, { 2, 4 }, { 3, 6 }, { 4, 8 } }; // throws a error

int[][] c = { { 1, 2, 3 }, { 11, 12, 13 } }; // throws error
Posted
Comments
Herman<T>.Instance 11-Nov-15 4:38am    
maybe int[][][] c?
Hitesh Jain 11-Nov-15 4:42am    
I Am Saying It About c#
Tomas Takac 11-Nov-15 4:44am    
You cannot initialize jagged arrays (b and c in your example) like this. Read documentation[^].

You got it wrong.
int[,] a
is a 2-dimensional array
while
int[][] b
is an array of arrays, i.e. each element is an array itself, e.g.
int[][] b = new int[5][] { new int[] {1,2}, new int[] {3,4}, new int[] {5,6}, new int[] {7,8}, new int[] {9,0} };

Read more https://msdn.microsoft.com/en-us/library/aa288453(v=vs.71).aspx[^]
 
Share this answer
 
v2
The following declaration creates a two-dimensional array of four rows and two columns.
int[,] array = new int[4, 2];
// 2d array
int[,] 2dArray = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };


The following is a declaration of a jagged-array : single-dimensional array that has three elements, each of which is a single-dimensional array of integers:
C#
int[][] jaggedArray = new int[3][];

Before you can use jaggedArray, its elements must be initialized. You can initialize the elements like this:
C#
jaggedArray[0] = new int[5];
jaggedArray[1] = new int[4];
jaggedArray[2] = new int[2];

Each of the elements is a single-dimensional array of integers. The first element is an array of 5 integers, the second is an array of 4 integers, and the third is an array of 2 integers.

From here:
Multidimensional Arrays[^]
Jagged Arrays[^]

-KR
 
Share this answer
 
The difference is that [,] is a 2D fixed size array containing exactly x * y elements, where x and y are given when you allocate the space in the new assignment:
C#
int[,] arr = new int[x,y];

[][] creates a jagged array, where you only assign one dimension at a time:
C#
int[][] arr = new int[x][];
for(int i = 0; i < x; i++)
   {
   arr[i] = new int[(i + 1) * 2];
   }
Each element of the jagged array can be a different size, and needs to be allocated with a separate new statement.

While you can initialize fixed size arrays with this syntax:
C#
int[,] arr = new int[5, 2] { { 0, 0 }, { 1, 2 }, { 2, 4 }, { 3, 6 }, { 4, 8 } };
You need this syntax to do the same with a jagged array:
C#
int[][] arr = new int[5][] { new int[] { 0, 0 }, 
                             new int[] { 1, 2 }, 
                             new int[] { 2, 4 }, 
                             new int[] { 3, 6 }, 
                             new int[] { 4, 8 } };
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900