For an indexer access, the primary-expression of the element-access must be a variable or value of a class, struct, or interface type, and this type must implement one or more indexers that are applicable with respect to the expression-list of the element-access.
The compile-time processing of an indexer access of the form P[A]
, where P
is a primary-expression of a class, struct, or interface type T
, and A
is an expression-list, consists of the following steps:
T
is constructed. The set consists of all indexers declared in T
or a base type of T
that are not override
declarations and are accessible in the current context (§3.3).S.I
in the set, where S
is the type in which the indexer I
is declared:
I
is not applicable with respect to A
(§7.4.2.1), then I
is removed from the set.I
is applicable with respect to A
(§7.4.2.1), then all indexers declared in a base type of S
are removed from the set.P
and an associated argument list of A
.Depending on the context in which it is used, an indexer access causes invocation of either the get-accessor or the set-accessor of the indexer. If the indexer access is the target of an assignment, the set-accessor is invoked to assign a new value (§7.13.1). In all other cases, the get-accessor is invoked to obtain the current value (§7.1.1).