C# Arrays

Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each 
value.
To declare an array, define the variable type with square brackets:
string[] cars;

We have now declared a variable that holds an array of strings.
To insert values to it, we can use an array literal - place the values in a comma-separated list, inside curly braces:
string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};

To create an array of integers, you could write:
int[] myNum = {10, 20, 30, 40};

You access an array element by referring to the index number.
This statement accesses the value of the first element in cars:
string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
Console.WriteLine(cars[0]);
// Outputs Volvo


Try it Yourself »

Note: Array indexes start with 0: [0] is the first element. [1] is the second 
element, etc.
To change the value of a specific element, refer to the index number:
cars[0] = "Opel";

string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
cars[0] = "Opel";
Console.WriteLine(cars[0]);
// Now outputs Opel instead of Volvo


Try it Yourself »

To find out how many elements an array has, use the 
Length property:
string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
Console.WriteLine(cars.Length);
// Outputs 4


Try it Yourself »

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.
The following example outputs all elements in the cars 
array:
string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
for (int i = 0; i < cars.Length; i++) 
{
  Console.WriteLine(cars[i]);
}
 


Try it Yourself »

There is also a foreach loop, which is used exclusively to loop through elements in an array:
foreach (type variableName in arrayName) 
{
  // code block to be executed
}

The following example outputs all elements in the cars 
array, using a foreach loop:
string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
foreach (string i in cars) 
{
  Console.WriteLine(i);
}
 


Try it Yourself »

The example above can be read like this: for each
string element (called 
i - as in 
index) in cars, print out the value of i.
If you compare the for loop and foreach loop, you will see that the foreach method is easier to write, it 
does not require a counter (using the Length property), and it is more readable.
There are many array methods available, for example Sort(), which sorts an array 
alphabetically or in an ascending order:
// Sort a string
string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
Array.Sort(cars);
foreach (string i in cars)
{
  Console.WriteLine(i);
}
 

// Sort an int
int[] myNumbers = {5, 1, 8, 9};
Array.Sort(myNumbers);
foreach (int i in myNumbers)
{
  Console.WriteLine(i);
} 


Try it Yourself »

Other useful array methods, such as 
Min, 
Max, and 
Sum, can be found in the System.Linq namespace:
using System;
using System.Linq;

namespace MyApplication
{
  class Program
  {
    static void Main(string[] args)
    {
      int[] myNumbers = {5, 1, 8, 9};
      Console.WriteLine(myNumbers.Max());  // returns the largest value
      Console.WriteLine(myNumbers.Min());  // returns the smallest value
      Console.WriteLine(myNumbers.Sum());  // returns the sum of elements
    }
  }
}
 
 
 
 


Try it Yourself »

You will learn more about other namespaces in a later chapter.
If you are familiar with C#, you might have seen arrays created with the new keyword, and perhaps you have seen arrays with a specified size as well. In C#, there are different ways to create an array:
// Create an array of four elements, and add values later
string[] cars = new string[4];

// Create an array of four elements and add values right away 
string[] cars = new string[4] {"Volvo", "BMW", "Ford", "Mazda"};

// Create an array of four elements without specifying the size 
string[] cars = new string[] {"Volvo", "BMW", "Ford", "Mazda"};

// Create an array of four elements, omitting the new keyword, and without specifying the size
string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};

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:
// Declare an array
string[] cars;

// Add values, using new
cars = new string[] {"Volvo", "BMW", "Ford"};

// Add values without using new (this will cause an error)
cars = {"Volvo", "BMW", "Ford"};


Try it Yourself »

Create an array of type string called cars.
  = {"Volvo", "BMW", "Ford", "Mazda"};

Start the Exercise
We just launchedW3Schools videos
Get certifiedby completinga course today!
If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:
help@w3schools.com
Your message has been sent to W3Schools.