PC Review


Reply
Thread Tools Rate Thread

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

 
 
=?UTF-8?B?TWFydGluIFDDtnBwaW5n?=
Guest
Posts: n/a
 
      5th Sep 2006
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
 
Reply With Quote
 
 
 
 
Morten Wennevik
Guest
Posts: n/a
 
      5th Sep 2006
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
<(E-Mail Removed)> wrote:

> 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]
 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      5th Sep 2006
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


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Array | Matrix - Formula??? aldo jr Microsoft Excel Discussion 0 13th Oct 2010 10:44 PM
Array/Matrix Egon Microsoft Excel Programming 5 11th Oct 2006 04:24 PM
Matrix/array question =?Utf-8?B?Y3Vyc2Vkbm9tb3Jl?= Microsoft Excel Misc 0 9th Nov 2005 08:09 PM
mirrored array/matrix hierarchii Microsoft Excel Misc 3 5th Aug 2005 09:35 AM
Lookup of sorts...Matrix Array etc. =?Utf-8?B?TWljaGVsZQ==?= Microsoft Excel Programming 5 14th Feb 2004 10:24 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:36 PM.