Reputation: 13
When we use the indexer property, we don't give it any parameter that will make this indexer property refer to the array that we want. What if we have more than one array?
How do we specify or force this property to refer to a specific array if we have more than one array in the class?
Upvotes: 0
Views: 514
Reputation: 22406
You have to use properties that take arguments, Which is not possible in C#, unfortunately
public string this[int index]
{
get { return namelist[index]; }
set { namelist[index] = value; }
}
The indexer points to the array variable you use inside the indexer.
Upvotes: 0
Reputation: 4992
Your understanding of an indexer may be slightly off. An indexer essentially doesn't have any relation to some underlying array. It merely provides a way to define a syntax similar to those used to access array elements.
In fact, the built-in Dictionary
class uses it with non-integer arguments to provide access to dictionary values via their corresponding key objects.
Also, indexers can take more than one parameter. You could, for example, define an enumeration of values representing the various arrays your class contains and return or set the appropriate value of the corresponding array, then.
Upvotes: 1