Home

The Array Class

   

Introduction to the Array Class

 

Overview

To assist with the use and management of arrays, you can combine the array features of the C# language and support from the .NET Framework. As mentionned in Lesson 28, to support arrays, the .NET Framework provides the Array class that is defined in the System namespace of the mscorlib.dll assembly.

When you create an array, you are in fact declaring a variable of type Array. Based on this, since an array variable is an object of a class type, you can use the characteristics of the Array class to create an array and/or to manipulate the values stored in the variable. You can create an array using any of the techniques we saw in the previous lessons, or you can use the Array class.

To assist you with creating an array, the Array class is equipped with the CreateInstance() method that comes in various versions. To create a one-dimensional array whose members are zero-based, you can use the following version:

public static Array CreateInstance(Type elementType, int length);

The first argument is used to specify the type of array you want to create. Since it is declared as Type, you can use the typeof operator to cast your type.

The second argument specifies the number of members of the array. Using the Array class, you can create an array as follows:

using System;

public class Exercise
{
    static int Main(string[] args)
    {
        Array numbers = Array.CreateInstance(typeof(double), length);

        return 0;
    }
}

You can also use the var keyword to declare the variable:

using System;

public class Exercise
{
    static int Main(string[] args)
    {
        var numbers = Array.CreateInstance(typeof(double), length);

        return 0;
    }
}

The Length of an Array

We know that if you declare a variable for an array but don't initialize it, you must specify the number of elements of the array. Here is an example:

using System;

public class Exercise
{
    static int Main(string[] args)
    {
        var numbers = new double[5];

        return 0;
    }
}

If you use the Array class to create an array, you must pass this constant integer as the second argument of the CreateInstance() method from the the above version. Here is an example:

using System;

public class Exercise
{
    static int Main(string[] args)
    {
        var numbers = Array.CreateInstance(typeof(double), 5);

        return 0;
    }
}

If the array exists already, that is, if you have already created the array or you are using an array created by someone else, to find out the number of items it contains, you can access its Length property. Therefore, the length of an array is the number of elements it contains.

Alternatively, you can call the Array.GetLength() method. Its syntax is:

public int GetLength(int dimension);

For a one-dimensional array, you must pass the argument as 0. This method returns a 32-bit integer that represents the number of items in the array.

The Rank of an Array

We have seen that the square brackets are used to specify that you are declaring an array. If you are creating a one-dimensional array, we saw that you could type a number in the square bracket. If you are creating a two-dimensional array, you type two numbers separated by a comma in the second pair of square brackets. Each number, whether it is one, two, or more is a placeholder for what is referred to a dimension. In other words, a one dimensional array has a dimension of one. A two-dimensional array has a dimension of 2.

To find out the dimension of an array, the Array class provides the Rank property. Therefore, to know the dimension of an existing array, you can access its Rank.

Fundamental Operations on an Array

 

Adding a Value to an Array

Before using a class, it must have values or members in it. In the previous lesson, we saw that, to initialize an array, you open the curly brackets and list its members separated by commas, or you could access each member and assign it the desired value. To support the ability to add members to an array, the Array is equipped with a method named SetValue() that comes in different versions. To add a new item to a the type of array we have used so far, you can call the following version of the Array.SetValue() method:

public void SetValue(object value, int index);

The first argument is the value to add to the list. The second argument is the index of the member to be added. The first item has index 1; the second item has index 2, and so on. Here is an example:

using System;

public class Exercise
{
    static int Main(string[] args)
    {
        var numbers = Array.CreateInstance(typeof(double), 5);

        numbers.SetValue(7628.937, 0);
        numbers.SetValue(6.48, 1);
        numbers.SetValue(574.9, 2);
        numbers.SetValue(293749.064, 3);
        numbers.SetValue(0.70257, 4);

        return 0;
    }
}

We indicated that whenever you create an array, you are in fact declaring an instance of the Array class. Therefore, even if you create an array using the square bracket formula we used in the previous lesson, you can call the SetValue() method to specify any member of the array. Here is an example:

using System;

public class Exercise
{
    static int Main(string[] args)
    {
        var numbers = new double[5];

        numbers.SetValue(7628.937, 0);
        numbers.SetValue(6.48, 1);
        numbers.SetValue(574.9, 2);
        numbers.SetValue(293749.064, 3);
        numbers.SetValue(0.70257, 4);

        return 0;
    }
}

The Array class provides a SetValue() version for each corresponding CreateInstance() method we reviewed earlier.

Getting a Value From an Array

Once the array is initialized, you can access its members and do what you want with their values. To support the ability to retrieve the value of a member of an array, the Array class is equipped with a method named GetValue that is overloaded with a version corresponding to each version of the CreateInstance() and the SetValue() methods. For example, to access the values stored in a one-dimensional array, you can call call this version:

public object GetValue(int index);

The index argument is the zero-based index of the member whose value you want to access. Here is an example:

using System;

public class Exercise
{
    static int Main(string[] args)
    {
        var numbers = new double[5];

        numbers.SetValue(7628.937, 0);
        numbers.SetValue(6.48, 1);
        numbers.SetValue(574.9, 2);
        numbers.SetValue(293749.064, 3);
        numbers.SetValue(0.70257, 4);

        Console.WriteLine("Number: {0}", numbers.GetValue(0));

        return 0;
    }
}

When calling the Array.GetValue() method, if you pass an invalid value, the compiler would throw an IndexOutOfRangeException exception.

Just as you can access one member of the array, you can access any member using its index. Here is an example that uses a for loop and the Length property to know the number of members of an array:

using System;

public class Exercise
{
    static int Main(string[] args)
    {
        //var numbers = Array.CreateInstance(typeof(double), 5);
        var numbers = new double[5];

        numbers.SetValue(7628.937, 0);
        numbers.SetValue(6.48, 1);
        numbers.SetValue(574.9, 2);
        numbers.SetValue(293749.064, 3);
        numbers.SetValue(0.70257, 4);

        for(int i = 0; i < numbers.Length; i++)
            Console.WriteLine("Number: {0}", numbers.GetValue(i));

        return 0;
    }
}

If using the foreach operator, you don't need the GetValue() method. Here is an example:

using System;

public class Exercise
{
    static int Main(string[] args)
    {
        var numbers = new double[5];

        numbers.SetValue(7628.937, 0);
        numbers.SetValue(6.48, 1);
        numbers.SetValue(574.9, 2);
        numbers.SetValue(293749.064, 3);
        numbers.SetValue(0.70257, 4);

        foreach(var number in Numbers)
            Console.WriteLine("Number: {0}", number);

        return 0;
    }
}

Sorting an Array

When you initialize an array, you add the members in an order of your choice. At one point, when accessing the members of an array, you may want them to be arranged in alphabetical, in numerical, or in chronological order. To assist you with re-arranging the elements in an array, the Array class is equipped with a method named Sort that is overloaded with as many versions as you can possibly need.

To arrange an array of the type we have used so far, you can call the following version of the Array.Sort() method:

public static void Sort(Array array);

This is a static method that takes as argument the name of the array you want to re-arrange. Here is an example:

using System;

public class Exercise
{
    static int Main(string[] args)
    {
        var numbers = new double[]
        {
            7628.937, 6.48, 574.9, 293749.064,
            0.70257, 314.905, 80458.01
        };

        Console.WriteLine("List of Numbers");
        foreach(var number in Numbers)
            Console.WriteLine("Number: {0}", Number);

        Array.Sort(Numbers);

        Console.WriteLine("\nList of Numbers");
        foreach (var number in Numbers)
            Console.WriteLine("Number: {0}", Number);

        return 0;
    }
}

This would produce:

List of Numbers
Number: 7628.937
Number: 6.48
Number: 574.9
Number: 293749.064
Number: 0.70257
Number: 314.905
Number: 80458.01

List of Numbers
Number: 0.70257
Number: 6.48
Number: 314.905
Number: 574.9
Number: 7628.937
Number: 80458.01
Number: 293749.064
Press any key to continue . . .

Notice that the numbers are arranged in in ascending order. In the same way, if the array is made of strings, you can call the Array.Sort() method to arrange it in alphabetical order. If the array is made of dates, you can arrange them in chronological order.

Reversing the Arrangement

To arrange the members in reverse order, you can call the Array.Reverse() method. Its syntax is:

public static void Reverse(Array array);

Here is an example of calling this method:

using System;

public class Exercise
{
    static int Main()
    {
        var numbers = new[] { 102, 44, 525, 38, 6, 28, 24481, 327, 632, 104 };

        Array.Reverse(numbers);

        foreach (int n in numbers)
            Console.WriteLine("{0} ", n);

        return 0;
    }
}

This would produce:

104
632
327
24481
28
6
38
525
44
102
Press any key to continue . . .

Finding an Item in an Array

The Array class provides various means of looking for, or locating an element in an array.

To support binary search, the Array class is equipped with a method named BinarySearch that is overloaded in 8 versions. One of the versions uses the following syntax:

public static int BinarySearch(Array array, object value);

Before calling this method, you sort the array. Here is an example of calling it:

using System;

public class Exercise
{
    static int Main()
    {
        var numbers = new[] { 102, 44, 525, 38, 6, 28, 24481, 327, 632, 104 };

        Array.Sort(numbers);

        foreach (int n in numbers)
            Console.WriteLine("{0} ", n);

        int index = Array.BinarySearch(numbers, 525);
        Console.WriteLine("The index of 525 is {0}", index);
        
        return 0;
    }
}

This would produce:

6
28
38
44
102
104
327
525
632
24481
The index of 525 is 7
Press any key to continue . . .

Locating the Index of an Element

One of the most routine operations you can perform on an array is to find out whether it contains this or that value. For example, if the array contains a certain member, you may want to retrieve the index of that member. To assist you with this, the Array class is equipped with a method named IndexOf() method that comes in various versions. To apply it on the type of array we have used so far, you can use the following syntax:

public static int IndexOf(Array array, object value);

This method visits each member of the array, looking for the value. Once it finds value in the array, it stops and returns the index where the first occurrence of value was found. Here is an example of calling it:

using System;

public class Exercise
{
    static int Main(string[] args)
    {
        var numbers = new double[]
        {
            7628.937, 6.48, 574.9, 293749.064,
            0.70257, 314.905, 80458.01
        };

        Console.WriteLine("List of Numbers");
        foreach(var number in Numbers)
            Console.WriteLine("Number: {0}", Number);
        Console.WriteLine();

        int Index = Array.IndexOf(Numbers, 314.905);

        Console.WriteLine("The index of 314.905 is {0}", Index);

        return 0;
    }
}

If the Array.IndexOf() method finds the value in the array, it returns its position. The above program produces:

List of Numbers
Number: 7628.937
Number: 6.48
Number: 574.9
Number: 293749.064
Number: 0.70257
Number: 314.905
Number: 80458.01

The index of 314.905 is 5
Press any key to continue . . .

If the array is not found, the method may return -1.

The IndexOf() method actually looks for the first occurrence of an item in an array. If you prefer to get the last occurrence of that item in the array, you can call the Array.LastIndexOf() method. It also is overloaded in three versions.

Deleting Items From an Array

Deleting an item array consists of removing it from the list. The Array class allows you to delete one element or a range of items from an array. To support these operations, the class is equipped with a static method named Clear. Its syntax is:

public static void Clear(Array array, int index, int length);

The first argument of this method is the name of the array on which the operation is performed. The second argument is the index of the item where the deletion will occur or start. If that second index exists in the array, the third argument specifies the number of items to remove. If an item is deleted, it receives a default value. For example, if it is a number, it gets a 0 value.

If you want to delete just one item, provide its index as the second argument and 1 as the third. Here is an example:

using System;

public class Exercise
{
    static int Main()
    {
        var numbers = new[] { 102, 44, 525, 38, 6, 28, 24481, 327, 632, 104 };

        foreach (int n in numbers)
            Console.Write("{0} ", n);

        Array.Clear(numbers, 2, 1);

        Console.WriteLine("\nAfter deleting the third item...");
        foreach (int n in numbers)
            Console.Write("{0} ", n);

        Console.WriteLine();
        return 0;
    }
}

This would produce:

102 44 525 38 6 28 24481 327 632 104
After deleting the third item...
102 44 0 38 6 28 24481 327 632 104
Press any key to continue . . .

To delete a range of items, pass the starting index as the second argument and pass the length of the range (the number of items to delete from the starting range) as the second argument. Here is an example:

using System;

public class Exercise
{
    static int Main()
    {
        var numbers = new[] { 102, 44, 525, 38, 6, 28, 24481, 327, 632, 104 };

        foreach (int n in numbers)
            Console.Write("{0} ", n);

        Array.Clear(numbers, 2, 3);

        Console.WriteLine("\nAfter deleting the third item...");
        foreach (int n in numbers)
            Console.Write("{0} ", n);

        Console.WriteLine();
        return 0;
    }
}

This would produce:

102 44 525 38 6 28 24481 327 632 104
After deleting the third item...
102 44 0 0 0 28 24481 327 632 104
Press any key to continue . . .

Clearing an array consists of deleting all of its elements and setting the count to 0. To do it, call the Array.Clear() method. Pass 0 as the second argument and the length of the array as the third argument. Here is an example:

using System;

public class Exercise
{
    static int Main()
    {
        var numbers = new[] { 102, 44, 525, 38, 6, 28, 24481, 327, 632, 104 };

        foreach (int n in numbers)
            Console.Write("{0} ", n);

        Array.Clear(numbers, 0, numbers.Length);

        Console.WriteLine("\nAfter deleting the third item...");
        foreach (int n in numbers)
            Console.Write("{0} ", n);

        Console.WriteLine();
        return 0;
    }
}

This would produce:

102 44 525 38 6 28 24481 327 632 104
After deleting the third item...
0 0 0 0 0 0 0 0 0 0
Press any key to continue . . .

Multidimensional Arrays

 

Two-Dimensional Arrays

The Array class supports the creation of any of the types of arrays we saw in the previous lessons. In the previous lesson, we saw that a two-dimensional array was an array made of two lists:

using System;

public static class Exercise
{
    public static int Main(string[] args)
    {
        var members = new string[List1Length, List2Length];

        return 0;
    }
}

To create such an array using the Array class, you can use the following version of the Array.CreateInstance() method:

public static Array CreateInstance(Type elementType, int length1, int length2)

The first argument is the type of array you want to create. The second argument is the length of the first list. The third argument is the length of the second list. Here is an example of using it:

using System;

public static class Exercise
{
    public static int Main(string[] args)
    {
        var members = Array.CreateInstance(typeof(string), 2, 4);

        return 0;
    }
}

To specify the values of a two-dimensional array, you can use the following version of the Array.SetValue() method:

public void SetValue(object value, int index1, int index2)

The first argument is the value you want to add. The second argument is the index of the list. The second argument is the index of the element that is being added. Here is an example:

using System;

public static class Exercise
{
    public static int Main(string[] args)
    {
        var members = Array.CreateInstance(typeof(string), 2, 4);
        
        members.SetValue("Celeste", 0, 0);  // 1st List - 1st Element
        members.SetValue("Mathurin", 0, 1); // 1st List - 2nd Element
        members.SetValue("Alex", 0, 2);     // 1st List - 3rd Element
        members.SetValue("Germain",0, 3);   // 1st List - 4th Element
        
	members.SetValue("Jeremy", 1, 0);   // 2nd List - 1st Element
        members.SetValue("Mathew", 1, 1);   // 1st List - 2nd Element
        members.SetValue("Anselme", 1, 2);  // 1st List - 3rd Element
        members.SetValue("Frederique", 1, 3);// 1st List - 4th Element
        
        return 0;
    }
}

Just as mentioned for the one-dimensional array, you can use the square brackets to create the array but call the SetValue() method to specify the value of each element.

To access a member of a two-dimensional array created with the Array.SetValue() method, you use the following version of the Array.GetValue() method:

public object GetValue(int index1, int index2)

This method takes two arguments. The first argument is the index of the list where the desired member resides. The second argument is the index of the element itself. Here is an example:

using System;

public static class Exercise
{
    public static int Main(string[] args)
    {
        var members = Array.CreateInstance(typeof(string), 2, 4);
        
        members.SetValue("Celeste", 0, 0);  // 1st List - 1st Element
        members.SetValue("Mathurin", 0, 1); // 1st List - 2nd Element
        members.SetValue("Alex", 0, 2);     // 1st List - 3rd Element
        members.SetValue("Germain",0, 3);   // 1st List - 4th Element
        
	members.SetValue("Jeremy", 1, 0);   // 2nd List - 1st Element
        members.SetValue("Mathew", 1, 1);   // 1st List - 2nd Element
        members.SetValue("Anselme", 1, 2);  // 1st List - 3rd Element
        members.SetValue("Frederique", 1, 3);// 1st List - 4th Element
        
        Console.WriteLine("Member: {0}", members.GetValue(0, 2));
        return 0;
    }
}

To access each member of the list, you can use two for loops. Use the first loop to access each list. Nest a second loop to it to access each member. To get the dimension of the main list, you can call the Array.GetLength() method and specify its argument as 0. For the internal loop, pass 1 as the argument to the Array.GetLength() method. Here is an example:

using System;

public static class Exercise
{
    public static int Main(string[] args)
    {
        var members = Array.CreateInstance(typeof(string), 2, 4);
        
        members.SetValue("Celeste", 0, 0);  // 1st List - 1st Element
        members.SetValue("Mathurin", 0, 1); // 1st List - 2nd Element
        members.SetValue("Alex", 0, 2);     // 1st List - 3rd Element
        members.SetValue("Germain",0, 3);   // 1st List - 4th Element
        
	members.SetValue("Jeremy", 1, 0);   // 2nd List - 1st Element
        members.SetValue("Mathew", 1, 1);   // 1st List - 2nd Element
        members.SetValue("Anselme", 1, 2);  // 1st List - 3rd Element
        members.SetValue("Frederique", 1, 3);// 1st List - 4th Element
        
        for(int List = 0; List < members.GetLength(0); List++)
            for(int Element = 0; Element < members.GetLength(1); Element++)
                Console.WriteLine("Member: {0}",
			members.GetValue(List , Element ));

        return 0;
    }
}

You can also use a foreach operator to access each member of the array. When using it, there is no need for a counter. Here is an example:

using System;

public static class Exercise
{
    public static int Main(string[] args)
    {
        var members = Array.CreateInstance(typeof(string), 2, 4);
        
        members.SetValue("Celeste", 0, 0);  // 1st List - 1st Element
        members.SetValue("Mathurin", 0, 1); // 1st List - 2nd Element
        members.SetValue("Alex", 0, 2);     // 1st List - 3rd Element
        members.SetValue("Germain",0, 3);   // 1st List - 4th Element
        
	members.SetValue("Jeremy", 1, 0);   // 2nd List - 1st Element
        members.SetValue("Mathew", 1, 1);   // 1st List - 2nd Element
        members.SetValue("Anselme", 1, 2);  // 1st List - 3rd Element
        members.SetValue("Frederique", 1, 3);// 1st List - 4th Element

        foreach (string Member in Members)
            Console.WriteLine("Member: {0}", Member);

        return 0;
    }
}

Three-Dimensional Arrays

Instead of two dimensions, you may want to create a three-dimensional arrays. A 3-D array is an array that, if created with the square brackets, would use two commas. Here is an example:

using System;

public static class Exercise
{
    static int Main(string[] args)
    {
        double[,,] Number;

        return 0;
    }
}

To create such an array using the Array class, you can use the following version of its CreateInstance() method:

public static Array CreateInstance(Type elementType,
				   int length1,
				   int length2,
 				   int length3)

Here is an example:

using System;

public static class Exercise
{
    static int Main(string[] args)
    {
        var number = Array.CreateInstance(typeof(double), 2, 3, 5);

        return 0;
    }
}

To specify the value of each member of the three-dimensional array, you can call the following version of the Array.SetValue() method:

public void SetValue (
	Object value,
	int index1,
	int index2,
	int index3
)

Here is an example:

using System;

public static class Exercise
{
    static int Main(string[] args)
    {
        var number = Array.CreateInstance(typeof(double), 2, 3, 5);

        number.SetValue(  12.44, 0, 0, 0);
        number.SetValue( 525.38, 0, 0, 1);
        number.SetValue(  -6.28, 0, 0, 2);
        number.SetValue(2448.32, 0, 0, 3);
        number.SetValue( 632.04, 0, 0, 4);
        number.SetValue(-378.05, 0, 1, 0);
        number.SetValue(  48.14, 0, 1, 1);
        number.SetValue( 634.18, 0, 1, 2);
        number.SetValue( 762.48, 0, 1, 3);
        number.SetValue(  83.02, 0, 1, 4);
        number.SetValue(  64.92, 0, 2, 0);
        number.SetValue(  -7.44, 0, 2, 1);
        number.SetValue(  86.74, 0, 2, 2);
        number.SetValue(-534.60, 0, 2, 3);
        number.SetValue( 386.73, 0, 2, 4);
        number.SetValue(  48.02, 1, 0, 0);
        number.SetValue( 120.44, 1, 0, 1);
        number.SetValue(  38.62, 1, 0, 2);
        number.SetValue( 526.82, 1, 0, 3);
        number.SetValue(1704.62, 1, 0, 4);
        number.SetValue(  56.85, 1, 1, 0);
        number.SetValue(105.48,  1, 1, 1);
        number.SetValue( 363.31, 1, 1, 2);
        number.SetValue( 172.62, 1, 1, 3);
        number.SetValue( 128.48, 1, 1, 4);
        number.SetValue( 906.68, 1, 2, 0);
        number.SetValue(  47.12, 1, 2, 1);
        number.SetValue(-166.07, 1, 2, 2);
        number.SetValue(4444.26, 1, 2, 3);
        number.SetValue( 408.62, 1, 2, 4);
        
        return 0;
    }
}

To get the value of each member of the three-dimensional array, you can call the following version of the Array.GetValue() method:

public Object GetValue (
	int index1,
	int index2,
	int index3
)

Here is an example:

using System;

public static class Exercise
{
    static int Main(string[] args)
    {
        var number = Array.CreateInstance(typeof(double), 2, 3, 5);

        number.SetValue(  12.44, 0, 0, 0);
        number.SetValue( 525.38, 0, 0, 1);
        number.SetValue(  -6.28, 0, 0, 2);
        number.SetValue(2448.32, 0, 0, 3);
        number.SetValue( 632.04, 0, 0, 4);
        number.SetValue(-378.05, 0, 1, 0);
        number.SetValue(  48.14, 0, 1, 1);
        number.SetValue( 634.18, 0, 1, 2);
        number.SetValue( 762.48, 0, 1, 3);
        number.SetValue(  83.02, 0, 1, 4);
        number.SetValue(  64.92, 0, 2, 0);
        number.SetValue(  -7.44, 0, 2, 1);
        number.SetValue(  86.74, 0, 2, 2);
        number.SetValue(-534.60, 0, 2, 3);
        number.SetValue( 386.73, 0, 2, 4);
        number.SetValue(  48.02, 1, 0, 0);
        number.SetValue( 120.44, 1, 0, 1);
        number.SetValue(  38.62, 1, 0, 2);
        number.SetValue( 526.82, 1, 0, 3);
        number.SetValue(1704.62, 1, 0, 4);
        number.SetValue(  56.85, 1, 1, 0);
        number.SetValue(105.48,  1, 1, 1);
        number.SetValue( 363.31, 1, 1, 2);
        number.SetValue( 172.62, 1, 1, 3);
        number.SetValue( 128.48, 1, 1, 4);
        number.SetValue( 906.68, 1, 2, 0);
        number.SetValue(  47.12, 1, 2, 1);
        number.SetValue(-166.07, 1, 2, 2);
        number.SetValue(4444.26, 1, 2, 3);
        number.SetValue( 408.62, 1, 2, 4);
        
        Console.WriteLine("Number: {0}\n", number.GetValue(0, 2, 4));

        return 0;
    }
}

This would produce:

Number: 386.73

Press any key to continue . . .

To access each member of the array, you can use three for loops. Here is an example:

using System;

public static class Exercise
{
    static int Main(string[] args)
    {
        var number = Array.CreateInstance(typeof(double), 2, 3, 5);

        number.SetValue(  12.44, 0, 0, 0);
        number.SetValue( 525.38, 0, 0, 1);
        number.SetValue(  -6.28, 0, 0, 2);
        number.SetValue(2448.32, 0, 0, 3);
        number.SetValue( 632.04, 0, 0, 4);
        number.SetValue(-378.05, 0, 1, 0);
        number.SetValue(  48.14, 0, 1, 1);
        number.SetValue( 634.18, 0, 1, 2);
        number.SetValue( 762.48, 0, 1, 3);
        number.SetValue(  83.02, 0, 1, 4);
        number.SetValue(  64.92, 0, 2, 0);
        number.SetValue(  -7.44, 0, 2, 1);
        number.SetValue(  86.74, 0, 2, 2);
        number.SetValue(-534.60, 0, 2, 3);
        number.SetValue( 386.73, 0, 2, 4);
        number.SetValue(  48.02, 1, 0, 0);
        number.SetValue( 120.44, 1, 0, 1);
        number.SetValue(  38.62, 1, 0, 2);
        number.SetValue( 526.82, 1, 0, 3);
        number.SetValue(1704.62, 1, 0, 4);
        number.SetValue(  56.85, 1, 1, 0);
        number.SetValue(105.48,  1, 1, 1);
        number.SetValue( 363.31, 1, 1, 2);
        number.SetValue( 172.62, 1, 1, 3);
        number.SetValue( 128.48, 1, 1, 4);
        number.SetValue( 906.68, 1, 2, 0);
        number.SetValue(  47.12, 1, 2, 1);
        number.SetValue(-166.07, 1, 2, 2);
        number.SetValue(4444.26, 1, 2, 3);
        number.SetValue( 408.62, 1, 2, 4);
        
        for(int external = 0; external < number.GetLength(0); external++)
            for(int internal = 0; internal < number.GetLength(1); internal++)
                for(int element = 0; element < number.GetLength(2); element++)
                    Console.WriteLine("Number: {0}",
                                 number.GetValue(external, internal, element));

        return 0;
    }
}

This would produce:

Number: 12.44
Number: 525.38
Number: -6.28
Number: 2448.32
Number: 632.04
Number: -378.05
Number: 48.14
Number: 634.18
Number: 762.48
Number: 83.02
Number: 64.92
Number: -7.44
Number: 86.74
Number: -534.6
Number: 386.73
Number: 48.02
Number: 120.44
Number: 38.62
Number: 526.82
Number: 1704.62
Number: 56.85
Number: 105.48
Number: 363.31
Number: 172.62
Number: 128.48
Number: 906.68
Number: 47.12
Number: -166.07
Number: 4444.26
Number: 408.62
Press any key to continue . . .

You can also use a foreach loop to access each member of the array. Here is an example:

using System;

public static class Exercise
{
    static int Main(string[] args)
    {
        var number = Array.CreateInstance(typeof(double), 2, 3, 5);

        number.SetValue(  12.44, 0, 0, 0);
        number.SetValue( 525.38, 0, 0, 1);
        number.SetValue(  -6.28, 0, 0, 2);
        number.SetValue(2448.32, 0, 0, 3);
        number.SetValue( 632.04, 0, 0, 4);
        number.SetValue(-378.05, 0, 1, 0);
        number.SetValue(  48.14, 0, 1, 1);
        number.SetValue( 634.18, 0, 1, 2);
        number.SetValue( 762.48, 0, 1, 3);
        number.SetValue(  83.02, 0, 1, 4);
        number.SetValue(  64.92, 0, 2, 0);
        number.SetValue(  -7.44, 0, 2, 1);
        number.SetValue(  86.74, 0, 2, 2);
        number.SetValue(-534.60, 0, 2, 3);
        number.SetValue( 386.73, 0, 2, 4);
        number.SetValue(  48.02, 1, 0, 0);
        number.SetValue( 120.44, 1, 0, 1);
        number.SetValue(  38.62, 1, 0, 2);
        number.SetValue( 526.82, 1, 0, 3);
        number.SetValue(1704.62, 1, 0, 4);
        number.SetValue(  56.85, 1, 1, 0);
        number.SetValue(105.48,  1, 1, 1);
        number.SetValue( 363.31, 1, 1, 2);
        number.SetValue( 172.62, 1, 1, 3);
        number.SetValue( 128.48, 1, 1, 4);
        number.SetValue( 906.68, 1, 2, 0);
        number.SetValue(  47.12, 1, 2, 1);
        number.SetValue(-166.07, 1, 2, 2);
        number.SetValue(4444.26, 1, 2, 3);
        number.SetValue( 408.62, 1, 2, 4);
        
        foreach(double n in number)
            Console.WriteLine("Number: {0}", n);

        return 0;
    }
}

Multidimensional Arrays

The Array class supports all dimensions of arrays beyond three. To create a multidimensional array, the class is equipped with the following version of its CreateInstance() method:

public static Array CreateInstance(Type elementType, params int[] lengths)

To add elements to the list, you can use the following equivalent version of the SetValue() method:

public void SetValue(object value, params int[] indices)

To get the value of an element, you would call the following version of the GetValue() method:

public Object GetValue(params int[] indices)

Getting Other Information About an Array

 

The Lower Bound of an Array

To better manage an array, the compiler must always be able to locate its highest and its lowest members. This is particularly important because an array must have a size.

The lowest member of an array can be located using the Array.GetLowerBound() method. Its syntax is:

public int GetLowerBound(int dimension);

The Upper Bound of an Array

The highest member of an array can be located using the Array.GetUpperBound() method. Its syntax is:

public int GetUpperBound(int dimension);

In both cases, the dimension argument is the rank of the array. For a single-dimensional array, as those we have always used so far, this parameter must have the value of 0.

 
 

Home Copyright © 2010-2011 FunctionX