Team LiB   Previous Section   Next Section

15.3 System.Array

C# implements arrays with the class System.Array. The Array class has a number of useful methods. Table 15-1 shows a few of the more important methods and properties of the System.Array class.

Table 15-1. Useful methods and properties of System.Array

Method or Property

Description

Clear()

Public static method that sets a range of elements in the array to zero or to a null reference

Copy()

Overloaded public static method that copies a section of one array to another array

IndexOf()

Overloaded public static method that returns the index (offset) of the first instance of a value in a one-dimensional array

LastIndexOf()

Overloaded public static method that returns the index of the last instance of a value in a one-dimensional array

Reverse()

Overloaded public static method that reverses the order of the elements in a one-dimensional array

Sort()

Overloaded public static method that sorts the values in a one-dimensional array

IsFixedSize

Public property that returns a value indicating whether the array has a fixed size

Length

Public property that returns the length of the array

Rank

Public property that returns the number of dimensions of the array

The Array class's static methods, Reverse() and Sort(), make manipulation of the objects within the array very easy. Note, however, that to reverse or sort the elements of the array, they must be of a type that implements the IComparable interface. (Chapter 16 describes the collection interfaces.)Chapter 15 The .NET Framework includes the String class, which does implement this interface, so we'll demonstrate both Reverse() and Sort() with Strings. The complete listing is shown in Example 15-9, followed by the output and analysis.

Example 15-9. Reverse() and Sort() methods of Array
using System;

namespace ReverseAndSort
{
   class Tester
   {

       public static void DisplayArray(object[] theArray)
       {

           foreach (object obj in theArray)
           {
               Console.WriteLine("Value: {0}", obj);
           }
           Console.WriteLine("\n");
       }

      public void Run()
      {
          String[] myArray = 
            {
                "Who", "is", "John", "Galt" 
            };

          Console.WriteLine("Display myArray...");
          DisplayArray(myArray);

          Console.WriteLine("Reverse and display myArray...");
          Array.Reverse(myArray); 
          DisplayArray(myArray);

          String[] myOtherArray = 
            {
                "We", "Hold", "These", "Truths",
                "To", "Be", "Self", "Evident", 
          };

          Console.WriteLine("Display myOtherArray...");
          DisplayArray(myOtherArray);

          Console.WriteLine("Sort and display myOtherArray...");
          Array.Sort(myOtherArray); 
          DisplayArray(myOtherArray);
      }

      [STAThread]
      static void Main()
      {
         Tester t = new Tester();
         t.Run();
      }
   }
}

 Output:
Display myArray...
Value: Who
Value: is
Value: John
Value: Galt

Reverse and display myArray...
Value: Galt
Value: John
Value: is
Value: Who

Display myOtherArray...
Value: We
Value: Hold
Value: These
Value: Truths
Value: To
Value: Be
Value: Self
Value: Evident

Sort and display myOtherArray...
Value: Be
Value: Evident
Value: Hold
Value: Self
Value: These
Value: To
Value: Truths
Value: We

The example begins by creating myArray, an array of strings containing the words:

"Who", "is", "John", "Galt"

This array is displayed and then passed to the Array.Reverse() method, where it is displayed again to see that the array itself has been reversed:

Value: Galt
Value: John
Value: is
Value: Who

Similarly, the example creates a second array, myOtherArray, containing the words:

"We", "Hold", "These", "Truths",
"To", "Be", "Self", "Evident",

which is passed to the Array.Sort() method. Then Array.Sort() happily sorts them alphabetically:

Value: Be
Value: Evident
Value: Hold
Value: Self
Value: These
Value: To
Value: Truths
Value: We

The method to display the strings has been made somewhat generic by declaring the type passed in to be an array of objects:

public static void DisplayArray(object[] theArray)

The DisplayArray() method iterates through the array of objects, passing each to WriteLine().

foreach (object obj in theArray)
{
    Console.WriteLine("Value: {0}", obj);
}

Since WriteLine() calls ToString() on objects, and since every object (including String) supports ToString(), declaring the temporary variable obj to be of type Object works very well. Using objects has the advantage that you can reuse your DisplayArray() method with arrays of other types of objects, once you know how to implement the IComparable interface (shown in Chapter 17).

    Team LiB   Previous Section   Next Section