0

there,

I use the following code to convert(? or should I call it reference?) a 2d array to a 1d array:

object[] newobj = (object[])obj;

What I am confused is, does this actually allocate a memory and copy the full "obj" to a new 1d array "newobj", or is newobj just another reference(or should I say, pointer) to the "obj"? Does it cost extra memory and copying time?

And another question, if this "obj" is only a single variable, not a array, will this code convert it to a 1d and length-equal-1 array?

Please clarify me with this.

Thanks in advance, alex

1 Answers1

0

Okay, I've taken the liberty to assume that you're referring to an array of arrays and not a 2d array as otherwise that cast will not compile

Multidimensional Array [][] vs [,]

to be fair that is pretty confusing!

When it comes to figuring out how code is working it never hurts just to have a fiddle. I knocked up some dummy code to show that all your doing is creating a new reference to the memory and not creating a copy.

    object[][] obj = new object[5][]; //Delcare Jagged array

    obj[0] = new object[] { 1, 1, 1 };
    obj[1] = new object[] { 2, 2, 2 };
    obj[2] = new object[] { 3, 3, 3 };
    obj[3] = new object[] { 4, 4, 4 };
    obj[4] = new object[] { 5, 5, 5 }; //Fill up with stuff

    Console.WriteLine("{0}", obj[2][0]);  // prints 3

    object[] newobj = (object[])obj; // Declare array of objects and cast 

    newobj[2] = new object[] { 9, 9, 9 }; // update using the new refrence

    Console.WriteLine("{0}", obj[2][0]); // print from old reference, now prints 9
    Console.WriteLine("{0}", ((object[])newobj[2])[0]); // print from new reference, prints 9 but requires a cast
    Console.WriteLine("{0}", newobj[2][0]); // Error, cannot apply indexing with [] to an expression of type object

So you can see that updating the array from the new 1d reference still updates the underlying memory which the original reference still points to. However, as you've insisted to the compiler that the second reference is to be a 1d array you can no longer use the [][] syntax, or at least not without a cast back again

You can also use a tool such as ILSpy to see how the code compiles. In this case our function declares two local types at the top

.locals init (
    [0] object[][],
    [1] object[]
)

but when the cast takes place, all the IL does is pushes the value (reference) of variable 0 onto the stack and pops it into variable 1, simple as that!

IL_00ca: ldloc.0  //push 0
IL_00cb: stloc.1  //pop 1
Kieran501
  • 191
  • 5
  • This is funny, but I actually means a 2d array instead of an array of arrays. You link actually enlighten me with these two concepts. However, since you said it would not compile if I assigned a 2d array to a 1d array, l am a bit confused. If I want to reference a 2d array like a 1d array, do I need to write a loop, to assign each element of the 2d array to the 1d array? This is way too trouble. – user2385238 Dec 23 '20 at 10:10
  • Yeah, the 2d array is pretty much a big list of objects (in your case) that just have some helpful syntax. If you want to make it 1d you'll have to jiggle about with loops and creating some new store for the second dimension (which is why you can't cast in this case, or at least i can't, maybe i'm being dumb?) Each object fundamentally references the same thing in each array, you wont copy the actual object, but if you change any reference in your new array it wont change in the old one and visa versa – Kieran501 Dec 23 '20 at 11:15
  • This has made things clear. I marked your comment as answer. – user2385238 Dec 23 '20 at 11:48