C# 学习者:访问数组 [索引] 值与直接值时实际发生了什么
C# learner: What actually happens when i access an array[indexed] value vs direct value
我喜欢了解事物是如何工作的,但我似乎找不到 google 的简单解释。
public class Understanding {
public int[] array = new[] {5, 6, 7};
public int x = 5;
public int y = 6;
public int z = 7;
}
例如,如果我访问数组 [0] 与仅访问 x 相比如何完成?
与 C 系列的任何其他语言(我猜几乎所有其他语言)相同:物理内存地址在运行时计算。
与简单局部变量的区别在于,对于局部变量,地址是固定的,而对于数组元素,它通过简单的公式计算:
[base address of array] + index * [size of array element]
我喜欢了解事物是如何工作的,但我似乎找不到 google 的简单解释。
public class Understanding {
public int[] array = new[] {5, 6, 7};
public int x = 5;
public int y = 6;
public int z = 7;
}
例如,如果我访问数组 [0] 与仅访问 x 相比如何完成?
与 C 系列的任何其他语言(我猜几乎所有其他语言)相同:物理内存地址在运行时计算。 与简单局部变量的区别在于,对于局部变量,地址是固定的,而对于数组元素,它通过简单的公式计算:
[base address of array] + index * [size of array element]