Make arrays with non-zero lower bounds in C#

In C#, arrays have 0 as their lower bounds. This is true for arrays declared as in the following:

string[] names = new string[10];
string[,] phone_numbers = new int[10, 2];

[arrays with non-zero lower bounds]
However, sometimes it would be more convenient to use an array with non-zero lower bounds. For example, suppose you want to record sales figures for the four quarters of the years 2000 through 2009. In that case, it might be nice to make an array with indexes that ranged from 2000 to 2009 (for the years) in the first dimension and from 1 to 4 (for the four quarters) in the second. In that case, you can make an Array object instead of a normal array.

The Array class has a CreateInstance method that makes an array. It has several overloaded versions, one of which takes as parameters two arrays of integers. The first array gives the number of elements that the array should have in each of its dimensions. The second array gives the lower bound for each dimension.

For example, the following code creates an Array of int. The first dimension has 10 elements and lower bound 2000. The second dimension contains 4 elements and lower bound 1.

// Make an array data[2000..2009, 1..4].
Array data = Array.CreateInstance(typeof(int),
    new int[] { 10, 4 },
    new int[] { 2000, 1});

Use the Array object’s SetValue and GetValue methods to get and set values. Use its GetLowerBound and GetUpperBound methods to learn about the object’s bounds.

The example program uses the following code to demonstrate this kind of array.

private void Form1_Load(object sender, EventArgs e)
{
    // Make an array data[2000..2009, 1..4].
    Array data = Array.CreateInstance(typeof(int),
        new int[] { 10, 4 },
        new int[] { 2000, 1});

    // Initialize the data randomly.
    Random rand = new Random();
    for (int year = 2000; year <= 2009; year++)
    {
        for (int quarter = 1; quarter <= 4; quarter++)
        {
            data.SetValue(rand.Next(10, 99), year, quarter);
        }
    }

    // Display the data.
    string txt = "Year\tQ1\tQ2\tQ3\tQ4";
    for (int year = data.GetLowerBound(0);
        year <= data.GetUpperBound(0); year++)
    {
        txt += "\r\n" + year.ToString();
        for (int quarter = data.GetLowerBound(1);
            quarter <= data.GetUpperBound(1); quarter++)
        {
            txt += string.Format("\t{0}",
                data.GetValue(year, quarter));
        }
    }
    txtData.Text = txt;
    txtData.Select(0, 0);
}

This code creates an Array as described earlier. It then loops through the array’s entries, using the SetValue method to place random values in the array.

The program then loops through the Array again, this time using GetLowerBound and GetUpperBound to learn the Array’s bounds. It writes the data into a string and displays the result in a TextBox.


Rant: Note that Visual Basic 6 had the ability to declare arrays with non-zero lower bounds as normal variables. This made using these arrays easy and extremely useful. Microsoft dropped support for that kind of array when it created .NET. It’s a shame because the mathematics behind implementing such an array is practically trivial, but using the Array class in this way is a big hassle. It was a bad decision on Microsoft’s part. (I suspect the only reason they did this is that C++ doesn’t have arrays with non-zero bounds.)


Download Example   Follow me on Twitter   RSS feed   Donate




This entry was posted in arrays and tagged , , , , , , , , , , , , . Bookmark the permalink.

One Response to Make arrays with non-zero lower bounds in C#

  1. Pingback: Make a class that wraps arrays with non-zero lower bounds in C#C# Helper

Leave a Reply

Your email address will not be published. Required fields are marked *