Zebra0.com

csharp arrays

The index to an array can be a variable. That makes it possible to use a loop to process the array.

int[] num= { 3, 5, 7, 1, 6 };
int total = 0;
for(int j = 0; j < num.Length; j++)
{
   total += num[j]; //Add each elemnt of the array to total
}
this.Text = "" + total;

Notice the use of num.Length in the for loop.
The length of the array is 5 (the number of elements), but they are numbered 0 to 4.
The loop adds together elements from num[0] (the first element) to num[4] (the last element.)

End of lesson, Next lesson: Dialogs in C#