Information Technology Reference
In-Depth Information
What Is an Indexer?
An indexer is a set of get and set accessors, similar to those of properties. Figure 6-15 shows
representations of an indexer for a class that can get and set values of type string .
Figure 6-15. Representations of an indexer
Indexers and Properties
Indexers and properties are similar in many ways.
￿
Like a property, an indexer does not allocate memory for storage.
￿
Both indexers and properties are used primarily for giving access to other data members
with which they are associated, and for which they provide set and get access.
-
A property is usually associated with a single data member.
-
An indexer is usually associated with multiple data members.
Note You can think of an indexer as a property that gives get and set access to multiple data members of
the class. You select which of the many possible data members by supplying an index, which itself can be
of any type—not just numeric.
Some additional points to be aware of when working with indexers are the following:
￿
An indexer can have either one or both of the accessors.
Indexers are always instance members. Hence, an indexer cannot be declared static .
￿
Like properties, the code implementing the get and set accessors does not have to be
associated with any fields or properties. The code can do anything, or nothing, as long as
the get accessor returns some value of the specified type.
￿
Search WWH ::




Custom Search