15

In C#, it's possible to initialize a multidimensional array using constants like so:

Object[,] twodArray = new Object[,] { {"00", "01", "02"}, 
                                      {"10", "11", "12"},
                                      {"20", "21", "22"} };

I personally think initializing an array with hard coded constants is kind of useless for anything other than test exercises. Anyways, what I desperately need to do is initialize a new multidimensional array as above using existing arrays. (Which have the same item count, but contents are of course only defined at runtime).

A sample of what I would like to do is.

Object[] first  = new Object[] {"00", "01", "02"};
Object[] second = new Object[] {"10", "11", "12"};
Object[] third  = new Object[] {"20", "21", "22"};
Object[,] twodArray = new Object[,] { first, second, third };

Unfortunately, this doesn't compile as valid code. Funny enough, when I tried

Object[,] twodArray = new Object[,] { {first}, {second}, {third} };

The code did compile and run, however the result was not as desired - a 3 by 3 array of Objects, what came out was a 3 by 1 array of arrays, each of which had 3 elements. When that happens, I can't access my array using:

Object val = twodArray[3,3];

I have to go:

Object val = twodArray[3,1][3];

Which obviously isn't the desired result.

So, is there any way to initialize this new 2D array from multiple existing arrays without resorting to iteration?

1
  • Does Object[] { first, second, third }; makes sense instead of Object[,] { first, second, third }; (remove dimension coma) ? should noted as twoarray[3][3] Commented Aug 26, 2011 at 13:36

4 Answers 4

9

This would work if you switched to jagged arrays:

int[] arr1 = new[] { 1, 2, 3 };
int[] arr2 = new[] { 4, 5, 6 };
int[] arr3 = new[] { 7, 8, 9 };

int[][] jagged = new[] { arr1, arr2, arr3 };

int six = jagged[1][2];

Edit To clarify for people finding this thread in the future

The code sample above is also inadequate as it results in an array of arrays (object[object[]]) rather than a jagged array (object[][]) which are conceptually the same thing but distinct types.

Sign up to request clarification or add additional context in comments.

5 Comments

I realize an array of arrays is possible, but there are an equal number of items in every row, and so a 2D array is really the structure I'm after. A multidimensional array creates a nice linear memory layout while a jagged array implies several extra levels of indirection.
Also, for the record, I attempted int[][] jagged = new[] { arr1, arr2, arr3 }; but what I got was an int[3][] (second dimension uninitialized) and the first 3 items are arrays. Might as well have just done int[] jagged = new[] { arr1, arr2, arr3 };
Fair point, in that case the answer is no, there is not a way to initialize a multi dimensional array in this manner exclusively using array initializer syntax :(
@Alain With this kind of set based programming you might find that a functional language like F# is more suited to your needs :)
Conceptually same thing from array consumer standpoint only. Aaand array of arrays could contain arrays of different lengths... Jagged "arrays" in C# are usually implemented as arr[a, b]
1

You are trying to assign array references to an array. For more details please read - Jagged Arrays.

Try this,

Object[] first = new Object[] { "00", "01", "02" };
Object[] second = new Object[] { "10", "11", "12" };
Object[] third = new Object[] { "20", "21", "22" };
Object[][] result = { first, second, third };

foreach (object [] ar in result)
   {
       foreach (object ele in ar)
        {
            Console.Write(" " + ele);
          }
       Console.WriteLine();
   }

Comments

0

I'm struggling to fully understand what you're really trying to achieve. If I got it right, you have some "lists" of strings, which you need to store in another list.

First of all, I'd recommend you to use a more modern approach than arrays. C# offers you IEnumerable<> and IList<> interfaces and all the stuff that derives from them, so no need to stick with old fashioned arrays.

You could do something like this:

var list1 = new List<string> { "foo1", "foo2", "foo3" };
var list2 = new List<string> { "foo4", "foo5", "foo6" };
var list3 = new List<string> { "foo7", "foo8", "foo9" };
var listOfStrings = new List<List<string>> { list1, list2, list3 };

Then if you want to access "foo6" you write:

var temp = listOfStrings[1][2];

2 Comments

since when are arrays old fashioned? Nearly all collection classes in .NET are little more than decorated arrays..
I was using strings as an example. I am trying to put together a 2D array of objects. Not a 1D array of arrays.
0

The following works just fine:

var a = new object[] { 0, 1, 1, 2 };
var b = new object[] { "0", "5", "0", "0" };
var c = new object[] { true, true, true, false };

object[][] m = new object[][] { a, b, c };


 var two = m[0][3];
 var bar = m[1][1];
 var f = m[2][3];

1 Comment

Almost a decade question. So, what is the difference of your answer to accepted answer?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.