Zebra0.com

csharp arraysIntroduction to Arrays

Introduction to Arrays

An array is like a list. This shows how to declare and change values in an array


Text of video
//This code declares 3 separate variable, 
//each has its own location in memory
int num1 = 3;
int num2 = 5;
int num3 = 7;

//Instead you could declare num to be an array with a size of 3:
int[] num = { 3, 5, 7 };

//If you want to change one of the values in an array, you must use the index:
num[0] = 12;
num[2] = 9;

//Instead of a consant as the index, you can use a variable:
int i = 1;
num[i] = 33;

//This is very powerful; you can use a loop to process an array:                      
//Set all the elements of num to 0:
for(int i=0;i<num.Length;i++)
{
   num[i] = 0;
}

NEXT: Variables as index of Arrays