开发者

Array of ValueType in C# goes to Heap or Stack? [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

(C#) Arrays, heap and stack and value types

I am trying to study some differences between memory allocation in c#

Let's suppose that I have this instruction:

int[] array = new int[512开发者_如何学运维];

Does the "array" go to the Heap? Or does it reserve 512 integers on Stack?


The array itself is memory allocated on the heap. People get confused and think value types are always on the stack and this is not always correct.

For example, in this class:

public class X
{
    public int Y;
}

Even though Y is an int (value type), since it is contained in a class (reference type), the int Y will be stored with the object itself, which is most likely on the heap.

The array is similar. The ints are contained in the array as value types, but the array itself is on the heap, and thus the ints are too.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜