17.2. Indexers

Arrays provide a convenient way to get and set a value of an element. Indexers allow a list of ordinary objects to be accessed as though they were arrays. Just as a property looks like a field but has methods to perform get and set operations, an indexer looks like an array but has methods to perform array indexing operations.

17.2.1. Indexers with an Integer Index

The class in Listing 17.3 shows an indexer with an integer index. The indexer is specified using this[..] syntax. The indexer has get and set blocks. If you remove the set block, the indexer becomes read-only. The return type is object, and again the keyword value appears in the set block. The index of the indexer is specified as the argument of this[..].

Listing 17.3 ...

Get .NET for Java Developers: Migrating to C# now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.