Saturday, August 25, 2007

Array > Iterating Through An Array

ITERATING THROUGH AN ARRAY
• Length property of System.Array tells the number of elements an array has
• An array can be iterated using for as well as foreach loop
• Sample code using for loop as under:
• int[] pins = {5,3,2,5};
for(int index = 0; index != pins.Length; index++)
{
int pin = pins[index]; Console.WriteLine(pin);
}
• Sample code using foreach loop
int[] pins = {5,3,2,1};
foreach(int pin in pins)
{ Console.WriteLine(pin); }

No comments: