0

i like understanding how things work and i can't seem to find a simple explanation with google.

public class Understanding { 
    public int[] array = new[] {5, 6, 7};
    public int x = 5;
    public int y = 6;
    public int z = 7;
}

for example, if i access array[0] how is that done compared to just accessing x?

AVOlight
  • 27
  • 9
  • 1
    So ClrCORE is now open source and available on GitHub. That source may answer your question best. Also, this questions seems to have good information: http://stackoverflow.com/questions/19370231/how-do-arrays-work-internally-in-c-c – Sam Axe Mar 08 '15 at 06:16
  • Search for beginner C tutorial - will show what happens in details... C# internal implementation of assignment for int is almost the same. – Alexei Levenkov Mar 08 '15 at 06:16

1 Answers1

3

The same as in any other language from the C-family (and almost all others, I guess): The physical memory address gets computed at runtime. The difference to a simple local variable is that for a local variable the address is fixed whereas for array elements it gets computed through the simple formula:

[base address of array] + index * [size of array element]
Thomas Weller
  • 11,631
  • 3
  • 26
  • 34
  • Thank YOU Thomas Weller !!! Very much appreciate it when one takes the time to explain something without any buzz :) – AVOlight Mar 08 '15 at 06:49