Arrays

Summary

An array stores multiple data values of the same data type in one and the same variable. This can be handy for loops because multiple variable can't be used for a loop but an arrays can.

In this tutorial the usage of the array will be explained with code examples.

Coding

Let us implement the array in the following situation:

We are searching for the number 16 in a list of integer variables.

Without the array we would have a lot of variables.

class Program
{
static void Main(string[] args)
{
int number1 = 4;
int number2 = 8;
int number3 = 15;
int number4 = 16;
int number5 = 23;

if (number1 == 16)
{
}
else if (number2 == 16)
{
}
else if (number3 == 16)
{
}
}
}

The syntax for the Array looks like this:    <DataType>[] <Variablenname> = new <DataType>[<Length of the Array>];

The brackets after the data type tells the program that it is an array.

To retrieve a value from the array:  <Variablenname>[<Index>]

The index starts with zero. That means that an array of the length 2 has the index 0 and 1.

Let's look at an example on how to create an array with values:

int[] numbers = new int;

numbers = 4;
numbers = 8;
numbers = 15;
numbers = 16;
numbers = 23;

Console.WriteLine(numbers);
Console.WriteLine(numbers.Length);

As you can see the initialization has roughly the same amount of lines.

But now the array can be run through with an array by using the index of the array.

A possibility to write the initialization shorter exists.

That way we don't have to specify the length of the array because the program counts the number of values and uses that as the length for the array.

int[] numbers = new int[] { 4, 8, 15, 16, 23, 42 };

string[] names = new string[] { "Eddie", "Alex", "Michael", "David Lee" };

This works for every data type. It is also possible to create an array of an array.

By using an index larger that the length of the array an exception will be thrown.

Let's try printing each value from the array into the console by using loops.

for (int i = 0; i < names.Length; i++)
{
Console.WriteLine(names[i]);
}

// Alternative zur For-Schleife
foreach (string name in names)
{
Console.WriteLine(name);
}

The array has a number of methods like a sort method or a method to reverse the array.

The String data type has a method called "ToCharArray()" because a string is literally a list of chars.

Like this we can print the sentence backwards into the console:

string zig = "You can get what you want out of life " +
" if you help enough other people get what they want.";

char[] charArray = zig.ToCharArray();
Array.Reverse(charArray);

foreach (char zigChar in charArray)
{
Console.Write(zigChar);
}

Complete program code:

class Program
{
static void Main(string[] args)
{
/*int number1 = 4;
int number2 = 8;
int number3 = 15;
int number4 = 16;
int number5 = 23;

if (number1 == 16)
{
}
else if (number2 == 16)
{
}
else if (number3 == 16)
{
}*/

/*int[] numbers = new int;

numbers = 4;
numbers = 8;
numbers = 15;
numbers = 16;
numbers = 23;

Console.WriteLine(numbers);
Console.WriteLine(numbers.Length);

int[] numbers = new int[] { 4, 8, 15, 16, 23, 42 };

string[] names = new string[] { "Eddie", "Alex", "Michael", "David Lee" };

for (int i = 0; i < names.Length; i++)
{
Console.WriteLine(names[i]);
}

foreach (string name in names)
{
Console.WriteLine(name);
}

string zig = "You can get what you want out of life " +
" if you help enough other people get what they want.";

char[] charArray = zig.ToCharArray();
Array.Reverse(charArray);

foreach (char zigChar in charArray)
{
Console.Write(zigChar);
}