14.5.6 Element access

An element-access consists of a primary-no-array-creation-expression,
followed by a ?[? token, followed
by an expression-list, followed by a ?]? token. The expression-list
consists of one or more expressions,
separated by commas.
element-access:
primary-no-array-creation-expression [ expression-list ]
expression-list:
expression
expression-list , expression
If the primary-no-array-creation-expression of an element-access is a value
of an array-type, the elementaccess
is an array access (?4.5.6.1). Otherwise, the
primary-no-array-creation-expression must be a
variable or value of a class, struct, or interface type that has one or
more indexer members, in which case the
element-access is an indexer access (?4.5.6.2).
14.5.6.1 Array access
For an array access, the primary-no-array-creation-expression of the
element-access must be a value of an
array-type. The number of expressions in the expression-list must be the
same as the rank of the array-type,
and each expression must be of type int, uint, long, ulong, or of a type
that can be implicitly converted
to one or more of these types.
The result of evaluating an array access is a variable of the element type
of the array, namely the array
element selected by the value(s) of the expression(s) in the
expression-list.
The run-time processing of an array access of the form P[A], where P is a
primary-no-array-creationexpression
of an array-type and A is an expression-list, consists of the following
steps:
?P is evaluated. If this evaluation causes an exception, no further steps
are executed.
?The index expressions of the expression-list are evaluated in order, from
left to right. Following
evaluation of each index expression, an implicit conversion (?3.1) to one
of the following types is
performed: int, uint, long, ulong. The first type in this list for which an
implicit conversion exists is
chosen. For instance, if the index expression is of type short then an
implicit conversion to int is
performed, since implicit conversions from short to int and from short to
long are possible. If
evaluation of an index expression or the subsequent implicit conversion
causes an exception, then no
further index expressions are evaluated and no further steps are executed.
?The value of P is checked to be valid. If the value of P is null, a
System.NullReferenceException is thrown and no further steps are executed.
C# LANGUAGE SPECIFICATION
144
?The value of each expression in the expression-list is checked against
the actual bounds of each
dimension of the array instance referenced by P. If one or more values are
out of range, a
System.IndexOutOfRangeException is thrown and no further steps are executed.
?The location of the array element given by the index expression(s) is
computed, and this location
becomes the result of the array access.
14.5.6.2 Indexer access
For an indexer access, the primary-no-array-creation-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-no-arraycreation-
expression of a class, struct, or interface type T, and A is an
expression-list, consists of the
following steps:
?The set of indexers provided by 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 (?0.5).
?The set is reduced to those indexers that are applicable and not hidden
by other indexers. The following
rules are applied to each indexer S.I in the set, where S is the type in
which the indexer I is declared:
If I is not applicable with respect to A (?4.4.2.1), then I is removed
from the set.
If I is applicable with respect to A (?4.4.2.1), then all indexers
declared in a base type of S are removed
from the set.
?If the resulting set of candidate indexers is empty, then no applicable
indexers exist, and a compile-time
error occurs. If the candidate indexers are not all declared in the same
type, the indexer access is
ambiguous, and a compile-time error occurs (this latter situation can only
occur for an indexer access on
an instance of an interface that has multiple direct base interfaces).
?The best indexer of the set of candidate indexers is identified using the
overload resolution rules of
?4.4.2. If a single best indexer cannot be identified, the indexer access
is ambiguous, and a compiletime
error occurs.
?The index expressions of the expression-list are evaluated in order, from
left to right. The result of
processing the indexer access is an expression classified as an indexer
access. The indexer access
expression references the indexer determined in the step above, and has an
associated instance
expression of 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 (?4.13.1). In all other cases, the
get-accessor is invoked to obtain the current
value (?4.1.1).
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值