foreach (object obj in theArray)
{
Console.WriteLine("Value: {0}", obj);
}
}
static void Main( )
{
// make an array of Employee objects
Employee[] myEmployeeArray = new Employee[3];
// initialize each Employee's vaue
for (int i = 0;i < 3;i++)
{
myEmployeeArray[i] = new Employee(i+5);
}
// display the values
PrintArray(myEmployeeArray);
// create an array of two strings
string[] array =
{
"hello", "world"
};
// print the value of the strings
PrintArray(array);
}
}
}
Output:
Contents of the Array Programming_CSharp.Employee[]
page 157
Programming C#
Value: 5
Value: 6
Value: 7
Contents of the Array System.String[]
Value: hello
Value: world
Example 9-7 begins by creating a simple Employee class, as seen earlier in the chapter. The Tester class now contains a new static method PrintMyArray(), which takes as a parameter a one-dimensional array of Objects:
public static void PrintMyArray(object[] theArray)
Object is the implicit base class of every object in the .NET Framework,. and so is the implicit base class of both String and Employee.
The PrintMyArray( ) method takes two actions. First, it calls the ToString( ) method on the array itself:
Console.WriteLine("Contents of the Array {0}",
theArray.ToString( ));
System.Array overrides the PrintMyArray() method to your advantage, printing an identifying name of the array:
Contents of the Array Programming_CSharp. Employee []
Contents of the Array System.String[]
PrintMyArray() then goes on to call ToString( ) on each element in the array it receives as a parameter. Because ToString( ) is a virtual method in the base class Object, it is guaranteed to be available in every derived class. You have overridden this method appropriately in Employee and so this works properly. Calling ToString( ) on a String object might not be necessary, but it is harmless and it allows you to treat these objects polymorphically.
9.2.5 System.Array
The Array class has a number of useful methods which extend the capabilities of arrays and make them smarter than arrays seen in other languages (see Table 9-1 earlier in this chapter). Two useful static methods of Array are Sort( ) and Reverse( ).These are fully supported for the built-in C#
types such as string. Making them work with Button is a bit trickier, as you must implement a number of interfaces (discussed in Chapter 8). Example 9-8 demonstrates the use of these two methods to manipulate String objects.
Example 9-8. Using Array.Sort and Array.Reverse
namespace Programming_CSharp
{
using System;
public class Tester
{
public static void PrintMyArray(object[] theArray)
{
foreach (object obj in theArray)
{
page 158
Programming C#
Console.WriteLine("Value: {0}", obj);
}
Console.WriteLine("\n");
}
static void Main( )
{
String[] myArray =
{
"Who", "is", "John", "Galt"
};
PrintMyArray(myArray);
Array.Reverse(myArray);
PrintMyArray(myArray);
String[] myOtherArray =
{
"We", "Hold", "These", "Truths",
"To", "Be", "Self", "Evident",
};
PrintMyArray(myOtherArray);
Array.Sort(myOtherArray);
PrintMyArray(myOtherArray);
}
}
}
Output:
Value: Who
Value: is
Value: John
Value: Galt
Value: Galt
Value: John
Value: is
Value: Who
Value: We
Value: Hold
Value: These
Value: Truths
Value: To
Value: Be
Value: Self
Value: Evident
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 with the words:
"Who", "is", "John", "Galt"
page 159
Programming C#
This array is printed, and then passed to the Array.Reverse( ) method, where it is printed 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
9.3 Indexers
There are times when it is desirable to access a collection within a class as though the class itself were an array. For example, suppose you create a list box control named myListBox that contains a list of strings stored in a one-dimensional array, a private member variable named myStrings. A list box control contains member properties and methods in addition to its array of strings.
However, it would be convenient to be able to access the list box array with an index, just as if the list box were an array. For example, such a property would permit statements like the following: string theFirstString = myListBox[0];
string theLastString = myListBox[Length-1];
An indexer is a C# construct that allows you to access collections contained by a class using the familiar [] syntax of arrays. An indexer is a special kind of property and includes get( ) and set(
) methods to specify its behavior.
You declare an indexer property within a class using the following syntax:
type this [type argument]{get; set;}
The return type determines the type of object that will be returned by the indexer, while the type argument specifies what kind of argument will be used to index into the collection that contains the target objects. Although it is common to use integers as index values, you can index a collection on other types as well, including strings. You can even provide an indexer with multiple parameters to create a multidimensional array!