Lesson 2.4 Arrays and Collections

Strictly speaking, this is an extension to how variables work. Yet it doesn’t really make sense unless taken within the context of loops and when you know a little bit about logic. They are essentially sets of variables all tied together. Peas in a pod, bullets on a belt and tennis balls in a tube are all good analogies.

But how do you put them to use? The code below displays all of the letters in the alphabet backwards.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Arrays1
{
    class Program
    {
        static void Main(string[] args)
        {
            char[] alphabet = new char[26];

            alphabet[0] = 'a';
            alphabet[1] = 'b';
            alphabet[2] = 'c';
            alphabet[3] = 'd';
            alphabet[4] = 'e';
            alphabet[5] = 'f';
            alphabet[6] = 'g';
            alphabet[7] = 'h';
            alphabet[8] = 'i';
            alphabet[9] = 'j';
            alphabet[10] = 'k';
            alphabet[11] = 'l';
            alphabet[12] = 'm';
            alphabet[13] = 'n';
            alphabet[14] = 'o';
            alphabet[15] = 'p';
            alphabet[16] = 'q';
            alphabet[17] = 'r';
            alphabet[18] = 's';
            alphabet[19] = 't';
            alphabet[20] = 'u';
            alphabet[21] = 'v';
            alphabet[22] = 'w';
            alphabet[23] = 'x';
            alphabet[24] = 'y';
            alphabet[25] = 'z';

            for (int x = 25; x >= 0; x--)
            {
                System.Console.WriteLine(alphabet[x]);
            }
            System.Console.ReadKey(true);
        }
    }
}

Let’s forget for a moment that there are quite a few ways of doing this much more easily and effectively. I’m setting this out to try and show how arrays can be cycled through.
It does this by declaring an array of characters, setting them up and then looping backwards through them.

The first step is to initialise the array of character variables.

char[] alphabet = new char[26];

This creates an array with space for 26 characters; the length of the alphabet, in the English one anyway. I’ve called it alphabet here but you could have called it Fred if you’d wanted. The next step is to set each of those characters.

    alphabet[0] = 'a';
    alphabet[1] = 'b';
    alphabet[2] = 'c';
    alphabet[3] = 'd';
    alphabet[4] = 'e';
    alphabet[5] = 'f';
    alphabet[6] = 'g';
    alphabet[7] = 'h';
    alphabet[8] = 'i';
    alphabet[9] = 'j';
    alphabet[10] = 'k';
    alphabet[11] = 'l';
    alphabet[12] = 'm';
    alphabet[13] = 'n';
    alphabet[14] = 'o';
    alphabet[15] = 'p';
    alphabet[16] = 'q';
    alphabet[17] = 'r';
    alphabet[18] = 's';
    alphabet[19] = 't';
    alphabet[20] = 'u';
    alphabet[21] = 'v';
    alphabet[22] = 'w';
    alphabet[23] = 'x';
    alphabet[24] = 'y';
    alphabet[25] = 'z';

Note the square [] brackets on each assigning. This is telling C# which character in the array you are setting. Another important part of arrays is that they start with zero. I have set alphabet[0] to be “a” rather than alphabet[1]. In some languages, this needn’t necessarily be the case. But in C# it is. This phenomenon is known as zero indexing a posh term for a pedestrian idea. Another upshot of this is that, while the array is 26 letters long, if you try to do anything with alphabet[26] it will throw an error. Yes, it goes 0 to 25, rather than 1 to 26.

Collections are a more sophisticated animal. They also put me in a rather difficult position. To cover collections properly would mean me needing to introduce the concept of objects. Not something I can cover just yet. Instead I shall go over the basic functional differences. The block of code below shows the reverse alphabet program being done using a list, a simple collection type object, instead of an array.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Arrays2
{
    class Program
    {
        static void Main(string[] args)
        {
            List<char> alphabet = new List<char>();
            alphabet.Add('a');
            alphabet.Add('b');
            alphabet.Add('c');
            alphabet.Add('d');
            alphabet.Add('e');
            alphabet.Add('f');
            alphabet.Add('g');
            alphabet.Add('h');
            alphabet.Add('i');
            alphabet.Add('j');
            alphabet.Add('k');
            alphabet.Add('l');
            alphabet.Add('m');
            alphabet.Add('n');
            alphabet.Add('o');
            alphabet.Add('p');
            alphabet.Add('q');
            alphabet.Add('r');
            alphabet.Add('s');
            alphabet.Add('t');
            alphabet.Add('u');
            alphabet.Add('v');
            alphabet.Add('w');
            alphabet.Add('x');
            alphabet.Add('y');
            alphabet.Add('z');

            for (int x = 25; x >= 0; x--)
            {
                System.Console.WriteLine(alphabet[x]);
            }
            System.Console.ReadKey(true);
        }
    }
}

The main are two main differences. The first is the way I created the collection. In the array example I had to tell it how many it was going to contain. For the collection, I just had to create it.

  List<char> alphabet = new List<char>(); 

Note I use a list for character variables. This could have been for strings or integers or any other type of variable. All I have had to have done is replace the variable name in between the <> brackets.

The second difference is the way you set the values of the collection. A collection is pretty easy going with what it holds compared to an array. You can add, and remove, values from a collection at will. This is useful when you don’t know how many you’re expecting in a list.

  alphabet.Add('m');

So, if collections have these two advantages over arrays, why bother with arrays? Truth is some people don’t. The main reason I would give is that arrays are less resource intensive. There is a lot of memory shifting and manipulation that goes on with a collection. This kind of trade off between convenience and performance is a common theme that goes through coding. Prepare for it to come up time and time again.


Supporting files

File Description Open with
Arrays 1 The code to show the alphabet backwards using arrays Visual C# 2008
Arrays 2 Same as Arrays 1, this shows the same process using a collection Visual C# 2008