[C# Helper]
Index Books FAQ Contact About Rod
[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[Beginning Software Engineering]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: 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];

[Make arrays with non-zero lower bounds in C#] 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 the example to experiment with it and to see additional details.

© 2009-2022 Rocky Mountain Computer Consulting, Inc. All rights reserved.