Junior — Middle
Kus asub automaatse mälu haldamise süsteemis esimene massiivi element?
sobes.tech AI
Vastus AI-lt
In C#, arrays are reference types and are stored in the managed heap. The first element of the array is located in memory immediately after the array's metadata (e.g., length).
When you create an array, the CLR allocates a block of memory on the heap, where the metadata of the array is stored first, followed by the array elements in sequence. Thus, the first element of the array is at an address offset from the start of the memory block by the size of the metadata.
This allows efficient access to array elements by index, as the elements are stored in a contiguous block of memory.
Example:
int[] numbers = new int[] {1, 2, 3};
// numbers points to an object in the heap where the elements are stored
int firstElement = numbers[0]; // access to the first element