Collections

Arrays

An Array is the most basic collection type available in the .NET Framework. An Array represents a sequential collection of items. Here is an example to get you started:

using System; public class Program { ...

 

When you declare an Array, you use a special alias of the form:

<type>[] names = new <type>[<size>];

Where  <type>  represents your target type, and  <size>  represents the size of your array (how many items you wish it to hold). Here are some examples:

int[] ages = new int[0]; // An empty array of integers.
string[] names = new string[1]; // An array of 1 capacity.
DateTime[] datesOfBirth = new DateTime[10]; // An array of 10 capacity.

Note, as with other declarations, you can also substitute the  var  keyword for the compiler will infer the type:

var droids = new Droid[5]; // Inferred as a 5-capacity array of Droid instances.

An alternative syntax for declaring an array and providing values is:

int[] ages = new int[] { 1, 2, 3, 4 };

With the above form, the compiler determines the number of items that will be added and generates the pure syntax seen previously. This is useful when you have a set of values that you want to create an array from easily:

string[] jedi = new string[] { "Luke", "Yoda" };

Accessing array values.

To access a value in an array, you use a notation known as "index notation", whereby you specify the index within the array of the value you are trying to obtain. To help you understand that, imagine that an Array in memory could be represented like follows:

So index-0 represents the first item in the array, index-1 is the second, etc. To put this into practice, to access the value, I need to use the index:

string name = names[0]; // Read the value at index-0
names[0] = name; // Write the value at index-0

Arrays have a fixed capacity, so, if you try an access an item which is out of the bounds of the array, you'll get an  ArgumentOutOfRangeException  thrown:

string[] names = names[10];
names[20] = "Luke"; // This will be an error.
names[-1] = "Leia"; // As will this.

Iterating through an Array

When you need to move through the array to perform some functions on each value, you can use a special syntax known as a "for loop". This is an iterator block which allows you to declare a variable used track the index as you move through the array.

for (int i = 0; i < array.Length; i++)
{
    string value = array[i];    
}

A for loop is defined using the following syntax:

for (<initializer>; <condition>; <iterator>)
    <body>

Where we can define what these are:

initializer - A statement (or set of statements) setting the intial state. This is executed before the loop is processed.
condition - A boolean expression that is evaluated on each pass of the loop.
iterator - A statement (or set of statements) that is executed at the end of each pass of the loop.

If the result of the condition is false, the loop ends. So when we are iterating over the contents of an array, we set the following parts:

initiaizer - int i = 0 - We are declaring a local-loop variable, of type Int32 named i, with an initial value of 0.
condition - i < array.Length - We are checking to make sure the value of i is less than the length of the array.
iterator - i++ we are increasing the value of i on each pass of the array.

If you haven't used the ++ operator before, it means to increment the value by 1. This is essentially equivalent to:

i = i + 1;

The return value of i++ is the value of i before it is incremented.

Page 2 of 31