// Add the indexer to the existing class definition.
public class PeopleCollection : IEnumerable
{
private ArrayList arPeople = new ArrayList();
// Custom indexer for this class.
public Person this[int index]
{
get { return (Person)arPeople[index]; }
set { arPeople.Insert(index, value); }
}
...
}
Beyond the use of the this keyword, the indexer looks just like any other C# property declara-
tion. For example, the role of the get scope is to return the correct object to the caller. Here, we are
in fact doing so by using the indexer of the ArrayList object! The set scope is in charge of placing
the incoming object into the container at the specified index; in this example, this is achieved by
calling the Insert() method of the ArrayList.
C H A P T E R 1 2
Do'stlaringiz bilan baham: |