473,498 Members | 2,023 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Array/Matrix Definition with [][] or [,]

Hello,
I´m new in programming with C# and having some troubles with
understanding the definitions of multi-dimensional arrays.

Is there any difference between defining an Array with [][] or [,]?
I tried to define it like this:

int[][] test = new int[list1.Count][list2.Count]

but this doesn´t work.
A definition like:

int[,] test = new int[list1.Count, list2.Count]
seems to work.
So I am asking myself now what is right and if there are any differences.

Or do I have a mistake in my first try to define an 2-dimensional array?

Regards,

Martin
Sep 5 '06 #1
2 17521
Hi Martin,

A multidimensional array is in C# defined with a , per dimension, so your
second definition is correct.

int[,] test = new int[list1.Count, list2.Count]
On Tue, 05 Sep 2006 11:16:23 +0200, Martin Pöpping
<ma******@despammed.comwrote:
Hello,
I´m new in programming with C# and having some troubles with
understanding the definitions of multi-dimensional arrays.

Is there any difference between defining an Array with [][] or [,]?
I tried to define it like this:

int[][] test = new int[list1.Count][list2.Count]

but this doesn´t work.
A definition like:

int[,] test = new int[list1.Count, list2.Count]
seems to work.
So I am asking myself now what is right and if there are any differences.

Or do I have a mistake in my first try to define an 2-dimensional array?

Regards,

Martin


--
Happy Coding!
Morten Wennevik [C# MVP]
Sep 5 '06 #2
If you want a square m-by-n array, then the [,] and [m,n] syntax is correct
and more convenient; [][] declares a /jagged/ array - i.e. an array of
arrays (which is useful if you don't need each row to have the same width),
but means that you mean to manually assign each row (to determine the
length) - for instance:

int[][] data = new int[3][]; // 3 rows
data[0] = new int[7]; // first row is 7 wide
data[1] = new int[9];
data[2] = new int[4];

Marc
Sep 5 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1867
by: Pjotr Wedersteers | last post by:
I have source information in a file, and (part of) this information comes as coordinates. A coordinate consists of a letter followed by a number, ranging from a1 to i9 I'd like to count the...
2
6484
by: hrmadhu | last post by:
Hi, I am trying to write a template for an array which I can use to create multi-dimensional matrices as // A 10x10 matrix with each element initialized to 0 Array< Array<int> > Matrix (10,0);...
7
2397
by: masood.iqbal | last post by:
I am having lots of trouble getting a simple program that initializs a dynamically allocated 2D array to work. My 2D array is not getting initialized properly, and additionally I am getting a...
4
9672
by: Stephen Mayes | last post by:
I have initialized an array like this. const char matrix = { {0, 1, 2, 3}, {0, 1, 2}, {0, 1} }; gcc, (with no options set,) errors unless I specify
10
2731
by: mark | last post by:
I have a simple windows application that has a function to read a csv file and enter the values into an array A as double(,). Also, an instance of form 2 (which has a DataGrid) is created and the...
2
3418
by: xhunga | last post by:
I have try a new version of my work. I have put the sizes of the matrix into the matrix. A = number of rows A = number of columns The first element of the matrix is A instead of A. You...
272
13865
by: Peter Olcott | last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a I think that the operator() member function does not work correctly, does anyone else know how to make a template for making two...
1
7944
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
152
9721
by: vippstar | last post by:
The subject might be misleading. Regardless, is this code valid: #include <stdio.h> void f(double *p, size_t size) { while(size--) printf("%f\n", *p++); } int main(void) { double array = { {...
0
6993
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7162
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7197
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
4584
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3088
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1411
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
650
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
287
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.