迭代一个数组
int[] arr = new int[] {1, 6, 3, 3, 9};
for (int i = 0; i < arr.Length; i++)
{
Console.WriteLine(arr[i]);
}
使用 foreach:
foreach (int element in arr)
{
Console.WriteLine(element);
}
使用指针进行不安全访问 https://msdn.microsoft.com/en-ca/library/y31yhkeb.aspx
unsafe
{
int length = arr.Length;
fixed (int* p = arr)
{
int* pInt = p;
while (length-- > 0)
{
Console.WriteLine(*pInt);
pInt++;// move pointer to next element
}
}
}
输出:
1
6
3
3
9