Home

The .NET Framework Support of Arrays

 

Introduction

The techniques we have used to create arrays so far are part of the traditional routines used in many languages. The Microsoft .NET Framework goes further than that. Every time you declare an array, it inherits from a class called Array that is defined in the System.dll assembly (which means you don't have to import any library in your project in order to use the Array class). The Array class provides support for any array using various properties and methods. After creating an array as we have done so far, you may want to rearrange the list based on one of the items. You may want to look for a particular item to find out whether it exists; then, if it exists, where it is located. The Array class provides many other routines to perform such operations.

Item Addition

As done so far, to add an item to a list, you specify its index and assign the desired value. Here is an example:

class Exercise
{
    static void Main()
    {
	string[] EmployeeName = new string[4];

	EmployeeName[0] = "Joan Fuller";
	EmployeeName[1] = "Barbara Boxen";
	EmployeeName[2] = "Paul Kumar";
	EmployeeName[3] = "Bertrand Entire";
    }
}

Alternatively, you can use the SetValue() method to add a new item to the list. This method has 8 versions. One of the versions of this method takes two argument that consist of an Object object and an integer. The first argument is the value that needs to be added to the list, the other argument(s) is(are) used for the index. Here is an example:

class Exercise
{
    static void Main()
    {
	string[] EmployeeName = new string[4];

	EmployeeName.SetValue("Joan Fuller", 0);
	EmployeeName.SetValue("Barbara Boxen", 1);
	EmployeeName.SetValue("Paul Kumar", 2);
	EmployeeName.SetValue("Bertrand Entire", 3);
    }
}
 

Item Retrieval

To locate an item of an array, we were using its index. Here is an example:

class Exercise
{
    static void Main()
    {
	string[] EmployeeName = new string[4];

	EmployeeName[0] = "Joan Fuller";
	EmployeeName[1] = "Barbara Boxen";
	EmployeeName[2] = "Paul Kouma";
	EmployeeName[3] = "Bertand Entire";

	Console.WriteLine("Employees Records");
	Console.WriteLine("Employee 1: {0}", EmployeeName[0]);
	Console.WriteLine("Employee 2: {0}", EmployeeName[1]);
	Console.WriteLine("Employee 3: {0}", EmployeeName[2]);
	Console.WriteLine("Employee 4: {0}", EmployeeName[3]);
    }
}

Alternatively, the Array class provides the GetValue() method, also loaded with 8 versions. Here is an example:

class Exercise
{
    static void Main()
    {
	string[] EmployeeName = new string[4];

	EmployeeName.SetValue("Joan Fuller", 0);
	EmployeeName.SetValue("Barbara Boxen", 1);
	EmployeeName.SetValue("Paul Kumar", 2);
	EmployeeName.SetValue("Bertrand Entire", 3);

	Console.WriteLine("Employees Records");
	Console.WriteLine("Employee 1: {0}", EmployeeName.GetValue(0));
	Console.WriteLine("Employee 2: {0}", EmployeeName.GetValue(1));
	Console.WriteLine("Employee 3: {0}", EmployeeName.GetValue(2));
	Console.WriteLine("Employee 4: {0}", EmployeeName.GetValue(3));
    }
}

 

The Length of an Array

Once an array has been created, you can use it as you see fit. We saw that, when creating an array, you can specify the number of its members, and you must do this if you are not initializing the array. As we saw already, if you are initializing an array when creating it, you don't have to specify its dimension. Here is an example:

using System;

namespace CSharpLessons
{
    class Exercise
    {
	static void Main()
	{
	    double[] number = new double[]{ 12.44, 525.38, 6.28, 2448.32, 632.04,
			                           -378, 48, 6348, 762, 83, 64, -7, 86,
				            534, -6, 386, 73, 5, -284, 3654, 671, 34, 693};

                    Console.WriteLine();
	}
}

Some time in your program, you may want to know how many members are in this type of array. You could get dizzy trying to count them and you may make a mistake with the count. All of the arrays used in C# derive from a class called Array. This class provides a property called Length. To find out how many members an array has, you can access its Length property. Here is an example:

using System;

namespace CSharpLessons
{
    class Exercise
    {
	static void Main()
	{
	    double[] number = new double[]{ 12.44, 525.38, 6.28, 2448.32, 632.04,
			                           -378, 48, 6348, 762, 83, 64, -7, 86,
				           534, -6, 386, 73, 5, -284, 3654, 671, 34, 693};
			
                    Console.WriteLine("Number of items {0}", number.Length);

                    Console.WriteLine();
	}
}

This would produce:

Number of items 23

The Dimensions of an Array

We have seen that the square brackets are used to specify to the compiler that you are declaring an array. If you are creating a one-dimensional array, we saw that you can 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 place holder 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, which is the parent of all arrays, provides the Rank property. Therefore, to know the dimension of an existing array, you can access its Rank.

 

The Bounds 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 fixed size (by the way, using some gymnastic, an array can grow or shrink, a feature that is not available in C/C++ but can be managed in C# using the .NET Framework).

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

public int GetLowerBound(int dimension);

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.

 

Previous Copyright © 2006-2016, FunctionX, Inc. Next