animalCollection[0] returns the first element in the collection by using an indexer. animalCollection[0].Feed() invokes the Feed method on the first element of the collection.
The book on page 285 states this:
Quote:
You can't, however, do the following:
animalCollection[0].Feed();
To access items via their indices in this way, you need to use an indexer.
|
animalCollection is of type Animals and so far an indexer is not defined. On page 286 an indexer is defined.
Code:
public Animal this[int animalIndex]
{
get
{
return (Animal)List[animalIndex];
}
set
{
List[animalIndex] = value;
}
}
To explain "this" in this context, the book states this - I'm just changing the order of the sentence:
The indexer looks much like any other property. The difference to properties is that the this keyword is used along with parameters in square brackets.
Hope this helps.