Professional Documents
Culture Documents
Create an Array
Arrays are used to store multiple values in a single variable, instead of declaring separate variables
for each value.
string[ ] cars;
To insert values to it, we can use an array literal - place the values in a comma-separated list,
inside curly braces:
cars[0] = "Opel";
string[ ] cars = {"Volvo", "BMW", "Ford", "Mazda"};
cars[0] = "Opel";
Console.WriteLine(cars[0]);
Array Length
To find out how many elements an array has, use the Length property:
Example
string[ ] cars = {"Volvo", "BMW", "Ford", "Mazda"};
Console.WriteLine(cars.Length);
It is up to you which option you choose. In our tutorial, we will often use the last option, as it is
faster and easier to read.
However, you should note that if you declare an array and initialize it later, you have to use
the new keyword:
string[ ] cars;
using System;
using System.Linq;
namespace ArrayMinMax {
class Program {
static void Main(string[ ] args) {
int[ ] numbers = {51, 1, 3, 4, 98};
Console.WriteLine("Smallest Element: " + numbers.Min());
Console.WriteLine("Largest Element: " + numbers.Max());
Console.ReadLine();
}
}
}
Example: Find the Average of an Array
using System;
using System.Linq;
namespace ArrayFunction {
class Program {
static void Main(string[ ] args) {
int[ ] numbers = {30, 31, 94, 86, 55};
float sum = numbers.Sum();
int count = numbers.Count();
float average = sum/count;
Console.WriteLine("Average : " + average);
Console.WriteLine("Average using Average() : " + numbers.Average());
Console.ReadLine();
}
}
}
Output
Average : 59.2
Average using Average() : 59.2
Here, we have also used the numbers.Average() method of the System.Linq namespace to
get the average directly.
Note: It is compulsory to use the System.Linq namespace while
using Min() , Max() , Sum() , Count() , and Average() methods.
Loop Through Arrays
Loop Through an Array
You can loop through the array elements with the for loop, and use the Length property to specify
how many times the loop should run.
Example
string[ ] cars = {"Volvo", "BMW", "Ford", "Mazda"};
for (int i = 0; i < cars.Length; i++)
{
Console.WriteLine(cars[i]);
}
Sort Arrays
Sort an Array
There are many array methods available, for example Sort(), which sorts an array alphabetically or
in an ascending order:
Example
string[ ] cars = {"Volvo", "BMW", "Ford", "Mazda"};
Array.Sort(cars);
for(int i=0;i<=cars.Length;i++)
{
Console.WriteLine(cars[i]);
}
Arrays can have any number of dimensions. The most common are two-dimensional arrays (2D).
Two-Dimensional Arrays
To create a 2D array, add each array within its own set of curly braces, and insert a comma (,)
inside the square brackets:
Example
int[ , ] numbers = { {1, 4, 2}, {3, 6, 8} };
This statement accesses the value of the element in the first row (0) and third column (2) of
the numbers array:
Example
int[ , ] numbers = { {1, 4, 2}, {3, 6, 8} };
The following example will change the value of the element in the first row (0) and first
column (0):
Example
int[ , ] numbers = { {1, 4, 2}, {3, 6, 8} };
numbers[0, 0] = 5; // Change value to 5
Console.WriteLine(numbers[0, 0]); // Outputs 5 instead of 1
Example
int[,] numbers = { {1, 4, 2}, {3, 6, 8} };
Console.WriteLine(i);
You can also use a for loop. For multidimensional arrays, you need one loop for each of the array's
dimensions.
Also note that we have to use GetLength() instead of Length to specify how many times the loop
should run:
Example
int[,] numbers = { {1, 4, 2}, {3, 6, 8} };
Console.WriteLine(numbers[i, j]);
}}