C# Arrays

We previously outlined how variables work: they hold one value. But what if you want to hold more than one value? That's where arrays come in. Arrays can hold multiple values of the same data type.

Syntax

<data type>[] arrayname;

or

Sponsored Links
<data type>[] arrayname = new <data type> [array size] {values};

The square brackets here mean that this is an array. Create a new C# project and name it Arrays.

Example

string [] car = new string [5]{"Audi", "BWM", "Ferrari", "Dodge", "Lexus"};

Console.WriteLine(car [1]); Console.ReadLine();

You should see it print out:

BMW

When you want to access a value from the array you use integer index. So when we called the first value from the array car we called car[1] it then printed out BMW. You might wonder why it did not print out Audi; this is because the list actually starts from 0 (0, 1, 2 3 etc.). Additionally, the numbers of values in the brackets (it's currently 5) must match the array size, so if you added another car company, for example Honda, it would give a compile-time error. When you declare the array type like 'string' or 'integer', you cannot store a double or any other data type.

Looping through arrays

Using a foreach statement you can loop through an array and get all the values.

Example

string[] cars = new string[] { "Audi", "BMW", "Toyota", "Honda", "Lexus" };

foreach (var car in cars)
{
 Console.WriteLine(car);
}
Console.ReadLine();

Code Explained

  • Create an array of strings
  • Use a foreach statement and loop through them
  • Print out each array

Sorting Arrays

Arrays can be sorted in order, either ascending or descending. You use the sort() method to sort the array.

Syntax

Array.Sort(arrayname);

Example

string[] cars = new string[] { "Audi", "BMW", "Toyota", "Honda", "Lexus" };

Array.Sort(cars);

foreach (var car in cars)
{
Console.WriteLine(car);
}
Console.ReadLine();

As before, this will output the arrays but in alphabetical order.

This time we will sort the arrays but descending:

string[] cars = new string[] { "Audi", "BMW", "Toyota", "Honda", "Lexus" };

Array.Sort(cars);
Array.Reverse(cars);

foreach (var car in cars)
{
Console.WriteLine(car);
}
Console.ReadLine();

The arrays here will be in descending order.

Array of any size

In the above example the array has a size of 5, however you can create an array of any size.

Syntax

string[] <array name> =  {array items};

Example

string[] chocolates = { "Snickers", "Bounty", "Twix", "Flake", "Twirl" };

Console.WriteLine(chocolates[0]);
Console.ReadLine();

The output for this will be Snipers.

Converting String to Array

You can use the ToCharArray() method to convert a string to an array.

Example

string text = "Hello World!";
char[] myText = text.ToCharArray();
Array.Reverse(myText);

Console.WriteLine(myText);
Console.ReadLine();

Code Explained

  • First we create a variable called 'text'
  • Next we take one letter and put it into an array
  • Then reverse the order and print out the result

Output

!dlroW olleH

Here text appears backwards.

Try It!

Summary

Arrays are useful when you want to store multiple values in a variable. This tutorial covered how to loop through arrays as well as perform basic array functions. See the C# and Visual Basic Slideshow; this small application primarily uses arrays.