0

Suppose we have the following code:

void method() {
    int[] test = new int[3];
    test[0] = 0;
    test[1] = 1;
    test[2] = 2;
}

From what I read from Jon Skeet's post on SO, the new int[3] part is equivalent to:

public class ArrayInt3 {
    public readonly int length = 3;
    public int value0;
    public int value1;
    public int value2;
}

So does that mean test (a reference to ArrayInt3) is on the stack? And does that mean ArrayInt3 is on the heap? And I suppose value0, value1, and value2 are on the heap as well (i.e. 0, 1, 2 in this example)?

So in total, there is 4 objects on the heap, correct?

trincot
  • 317,000
  • 35
  • 244
  • 286

1 Answers1

1

There will be no object on the stack. On the stack, there will be a reference value to the single int[] object stored in the heap.

You have to start making the distinction between objects, variables, and values.

Local variables are part of the method stackframe, which is on the stack. So the reference value to the int[] will be stored in the variable, on the stack.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724