What is the difference between storing in memory arrays from the values ​​of the significant and reference type?

  • There are ideas, but we need a clear, specific and correct answer - Alexander
  • @MBo is a question that an experienced programmer may not know the answer to (for example, if you haven’t studied the features of Sharp). It would be something to send to the manuals. I think that this question should be. Perhaps even with the C # Faq tag and quibbles are unnecessary. - Andrew

3 answers 3

An array is a continuous area of ​​memory in which several elements are sequentially arranged one after the other. An array has a header, about which, normally, no one talks (and does not know), and the actual value.

If we are talking about significant types (structures), they are placed directly in the area designated for the array.

In the case of reference types (classes), their instances are placed on the heap, and the array contains all the same structures (IntPtr) - pointers to these objects.

Unlike an array of significant types, an array of reference types can contain elements pointing to the same object.

enter image description here

  • And the arrays themselves are always in .Net being a reference type regardless of the data stored in it. And placed in a managed heap. - Andrew
  • one
    @Andrew if we are talking about managed arrays, of course. For unmanaged, there is int* array = stackalloc int[100]; - Lunar Whisper

If we ignore the specifics of C # arrays (which are referential), then a variable of a significant type stores the value itself, and a variable of a reference type stores the address of a value. The value itself is stored in dynamic memory (heap).

    All arrays are reference types.

    Arrays mechanisms for the collection of items. The Microsoft® .NET Common Language Runtime (CLR) supports single-dimensional arrays, multidimensional arrays, and jagged arrays (arrays of arrays). All array types are derived from System.Array, which itself is derived from System.Object. It means that it ’s not a problem.

    https://stackoverflow.com/questions/1533757/is-int-a-reference-type-or-a-value-type https://docs.microsoft.com/en-us/previous-versions/dotnet/articles /bb985948 (v=msdn.10)

    • The question is not about that - MBo
    • on "Stack Overflow in Russian" I would like to see the answers in Russian - 4per
    • @ 4per 1snik? gave a brief answer in Russian, detailed - copied from off site (so as not to lose detail when translating) - pasha goroshko
    • @pashagoroshko, no, no. Answers in English, I and everyone else can read on enSO. - 4per