Multidimensional Rectangular Arrays

C# and the CLR contain direct support for multidimensional arrays, also known as rectangular arrays. You can easily declare an array with multiple rank within C#. Simply introduce a comma into the square brackets to separate the rank, as shown in the following example:

using System;
public class EntryPoint
{
static void Main() {
int[,] twoDim1 = new int[5,3];
int[,] twoDim2 = { {1, 2, 3},
{4, 5, 6},
{7, 8, 9} };
foreach( int i in twoDim2 ) {
Console.WriteLine( i );
}
}
}

There are several things to note when using rectangular arrays. All usage of these arrays boils down to method calls on a CLR-generated reference type, and the built-in vector types don’t come into play here. Notice the two declarations. In each case, you don’t need the size of each dimension when declaring the type. Again, that’s because arrays are typed based on their containing type and rank. However, once you create an instance of the array type, you must provide the size of the dimensions. I did this in two different ways in this example. In creating twoDim1, I explicitly said what the dimension sizes are, and in the creation of twoDim2, the compiler figured it out based upon the initialization expression.

In the example, I listed all of the items in the array using the foreach loop as shown. foreach iterates over all items in the array in a row-major fashion. I could have achieved the same goal using two nested for loops, and I definitely would have needed to do that if I needed to iterate over the array elements in any other order. When doing so, keep in mind that the Array.Length property returns the total amount of items in the array. In order to get the count of each dimension, you must call the Array.GetLength method supplying the dimension that you’re interested in. For example, I could have iterated over the items in the array using the following syntax, and the results would have been the same:

using System;
public class EntryPoint
{
static void Main() {
int[,] twoDim = { {1, 2, 3},
{4, 5, 6},
{7, 8, 9} };
for( int i = 0; i != twoDim.GetLength(0); ++i ) {
for( int j = 0; j != twoDim.GetLength(1); ++j ) {
Console.WriteLine( twoDim[i,j] );
}
}
for( int i = twoDim.GetLowerBound(0);
i <= twoDim.GetUpperBound(0);
++i ) {
for( int j = twoDim.GetLowerBound(1);
j <= twoDim.GetUpperBound(1);
++j ) {
Console.WriteLine( twoDim[i,j] );
}
}
}
}

For good measure, I’ve shown how to iterate over the dimensions of the array using two methods. The first method assumes that the lower bound of each dimension is 0, and the second does not. In all of the calls to GetLength, GetUpperBound, and GetLowerBound, you must supply a zero-based dimension of the Array that you’re interested in.

When you access the items of a multidimensional array, the compiler generates calls to Get and Set methods, which are similar to GetValue and SetValue. These methods are overloaded to accept a variable list of integers to specify the ordinal of each dimension within the array.

When mapping multidimensional arrays to mathematical concepts, the rectangular array is the most natural and preferred way to go. However, creating methods where an argument may be an array of varying rank is tricky, because you must accept the argument as type System.Array and dynamically deal with the rank of the array. You can access the rank of an array using the Array.Rank property. Thus, creating rank-general code is tricky due to the syntactical burden of accessing all array items through method calls to System.Array, but it is entirely possible. Moreover, the most general array-manipulation code should also handle the case of nonzero lower bounds in the individual ranks.

All arrays created within C# using the standard C# array declaration syntax will have a lower bound of 0. However, if you’re dealing with arrays used for mathematical purposes, as well as arrays that come from assemblies written in other languages, you may need to consider that the lower bound may not be 0.

Source Of Information : Apress Accelerated C Sharp 2010

0 comments


Subscribe to Developer Techno ?
Enter your email address:

Delivered by FeedBurner