Click here to Skip to main content
15,884,722 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I got this in my project:- object[,] objData = new Object[rowCnt, 20] ...

Please let me know why comma(,) is there in square brackets after Object.
and what "rowCnt" and "20" specify in the above line of code...
Posted
Updated 3-Aug-15 19:56pm
v3
Comments
[no name] 4-Aug-15 6:39am    
If the code is in your project, you must have written it. Why are you writing code that you don't know what it means?
Member 11579819 4-Aug-15 13:50pm    
I am into Maintenance.. That is why I didn't knew the meaning of the above code lines.

1 solution

OK: it specifies that this is a two dimensional array.

C#
object[] ar = new object[5];
creates an array of 5 objects - this is a one dimensional array because to access it you only need to provide one coordinate, or index:
C#
object o1 = ar[0];
object o2 = ar[1];
object o3 = ar[2];
object o4 = ar[3];
object o5 = ar[4];
When you add a comma, you create a two dimensional array:
C#
object[,] ar = new object[3, 5];
This creates an array of 15 objects - three rows by five columns - and it requires two coordinates or indexes to access:
C#
object o1 = ar[0, 0];
object o2 = ar[0, 1];
object o3 = ar[0, 2];
object o4 = ar[0, 3];
object o5 = ar[0, 4];
object o6 = ar[1, 0];
object o7 = ar[1, 1];
object o8 = ar[1, 2];
object o9 = ar[1, 3];
object o10 = ar[1, 4];
object o11 = ar[2, 0];
object o12 = ar[2, 1];
object o13 = ar[2, 2];
object o14 = ar[2, 3];
object o15 = ar[2, 4];
There is also a Jagged array, where the number of columns for each row are not the same:
C#
object[][] ar = new object[3];
ar[0] = new object[5];
ar[1] = new object[2];
ar[2] = new object[1024];
Again, this requires to indexes, but this time you have to be a lot more carefull!
C#
object 01 = ar[0][3];
is fine, but
C#
object o1 = ar[1][3];
will give you an error because the second row does not contain enough elements!
 
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