Iterate over an array C# with Example



Iterate over an array C# with Example

int[] arr = new int[] {1, 6, 3, 3, 9}; 
for (int i = 0; i < arr.Length; i++) 
{ 
Console.WriteLine(arr[i]); 
} 
using foreach: 
foreach (int element in arr) 
{ 
Console.WriteLine(element); 
} 
 

using unsafe access with pointers 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 
} 
} 
} 
Output: 
1 
6 
3 
3 
9 

0 Comment's

Comment Form

Submit Comment