2

Hopefully this is not a duplicate as I did do a search and found nothing.

At first glance I thought they would behave similarly but no. One is a 2 dimensional array of string and the other is an array of string arrays, i.e. what you get back from (List<string[]>)obj.ToArray().

I know how to return the jagged array type using (List<string[]>)obj.ToArray().

How can I return 2d array using a similar concept, i.e. build up a collection object from other data and call ToArray or other method that will return a 2d array.

Thanks

  • What is the difference between a 2d array of strings and an array of string arrays? I don't see one... at least not in utility. – evanmcdonnal Jun 27 '13 at 18:27
  • [**Arrays (C# Programming Guide)**](http://msdn.microsoft.com/en-us/library/9b9dty7d.aspx) – p.s.w.g Jun 27 '13 at 18:27
  • @evanmcdonnal `new[] { new string[1], new string[2] }`, that's possible with `string[][]` but not with `string[,]`. –  Jun 27 '13 at 18:27
  • classic case of diplopia – Pinch Jun 27 '13 at 18:28
  • @hvd Ah, I see the difference now. I would never do that though... and I'd probably call you an idiot if you did :-&( – evanmcdonnal Jun 27 '13 at 18:29
  • @evanmcdonnal Creating a jagged array when given a number of single dimensional arrays can be a useful operation; and it notably doesn't require copying the data the way a multidimensional array would. You can also do the reverse; pull out an array and then expose it externally without needing to copy the elements. – Servy Jun 27 '13 at 18:30
  • @evanmcdonnal Why not? You expect to be able to store strings of different lengths in a `string[]`, would you not? What's so different about arrays of different lengths? It doesn't always make sense, but when it does, there's no reason to avoid it. –  Jun 27 '13 at 18:31
  • @Servy actually that makes sense but I'm typically using `List` in code like that. – evanmcdonnal Jun 27 '13 at 18:33

2 Answers2

8

One is a jagged array, where the other is a multidimensional array.

There are some differences. A multidimensional array will guarantee each row having the length. With a jagged array, each "row" is an array, and those arrays can be varying sizes.

Another difference is in memory layout. A multidimensional array is a single object, the array elements are guaranteed to be closer together in memory. Since a jagged array, is an array of arrays, there is no guarantee that the each array will be allocated sequentially in memory, particularly in multi-threaded situations.

It is also good to note that the .NET Framework Design Guidelines for Array Usage suggests to use jagged arrays over multidimensional:

√ CONSIDER using jagged arrays instead of multidimensional arrays.

Christopher Currens
  • 29,917
  • 5
  • 57
  • 77
1

jagged array is string[][] 2d array is string[,]

In other words with string[,] each row has the same number of elements. in string[][] each row may have a different number of elements.

Eric Hartford
  • 16,464
  • 4
  • 33
  • 50