Home » C# Basics » 04 - C# Programming - Part 2
4

Arrays

Defines what arrays are and how it is used in a C# program

So far we have worked with variables. Variables are used for storing a single value. Consider a situation where we need to store multiple values such as the salaries of 10 employees. We can create 10 different variables but using so many variables can be problematic. What if we need to store the salaries of 100 or 500 employees?

Arrays provide a solution for this problem. Using an array, we can store multiple values under a single name.

Arrays need to be declared before they can be used. The syntax is:

data-type[ ] Array-name=new data-type[size];

For example,

int[ ] salary=new int[10];

The above declaration creates an array. The name of the array is `salary'. The size of the array is 10 which means we can store 10 values in the array. The data type of the array is int. It means that all the 10 values in the array should be of the integer type.

salary

Values-

5000

6000

5800

6500

7000

8200

6400

5520

7700

6900

Index-

0

1

2

3

4

5

6

7

8

9

The figure above helps to illustrate what an array is. Our example array `salary' has 10 values stored in it. Each value represents a salary. Each value has an index number associated with it. The index number starts with 0 and the last index number is 9, making it a total of 10 numbers.

The individual array elements (values) are accessed using their index numbers. For example, salary[0] represents the first value - 5000. Similarly, salary[7] represents the value 5520. In general terms, we can say that the individual element of an array can be represented as array-name[index-number].

A loop statement can be used to go through all the elements of an array. Let us consider our `salary' array that contains the salary of 10 employees. Suppose each employee gets a bonus and this bonus must be added to his or her salary. We can do it in this way:

salary[0]=salary[0]+200; //the bonus, added to the salary of the first employee.
salary[1]=salary[1]+200;
salary[2]=salary[2]+200;
.
.
.
salary[9]=salary[9]+200;

This is a time consuming process. What if the array contains 100 or 500 elements?

A better approach is to use a loop-

for (int i=0; i<10; i++)
{
salary[i]=salary[i]+200;
}

Here, the integer variable `i' represents the index of the array. The loop will start with the value of `i' as 0 and continue till it gets to 9. Remember that the index numbers for our array are from 0 to 9. Inside the loop, 200 is added to each element of the array.

Another loop statement that can be used with arrays is the `foreach' loop. However, it is recommended that we do not make any changes to the array contents (values) inside the foreach loop. This loop is mainly used for reading the values of the array or processing the array in situations where the values if the array are not changed.

foreach( int s in salary)
{
//code inside the loop
}

Let us make a complete program to understand how arrays work. In this example, we will ask the user to enter some elements in an array. Our program will then find the largest element from the array.

Start a new console project and type the following code inside the Main method:

 static void Main(string[] args) { int[] arr = new int[5]; int i, max;
 Console.WriteLine("Enter 5 values in the array"); 
for (i = 0; i < 5; i++) 
{ arr[i] = int.Parse(Console.ReadLine());
 } 
max = arr[0];
for (i = 1; i < 5; i++)
 {
 if (arr[i] > max)
 { 
max = arr[i]; 
} 
} 
Console.WriteLine("The largest element in the array is {0}", max); 
}

In this code, an integer array has 5 elements. Two other variables - i (for loop counter) and max (to store the largest value) are declared.

The first `for' loop is used to store 5 values in the array. After the loop, we have assigned the first element of the array (arr[0]) into the variable max. We will now compare this value with the other elements of the array. If any element is found larger than max, that element will be assigned to max.

This is exactly what is being done in the second `for' loop. Each array element is compared with the variable max. If any element is larger than the value of max, that value is assigned to the max. If not, the loop continues with the next element of the array.

At the end of this loop, the variable max contains the largest value from the array. This is displayed by using an output statement.