C#遍历数组
示例
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/zh-cn/library/y31yhkeb.aspx
unsafe
{
int length = arr.Length;
fixed (int* p = arr)
{
int* pInt = p;
while (length-- > 0)
{
Console.WriteLine(*pInt);
pInt++;//将指针移到下一个元素
}
}
}输出:
1
6
3
3
9