14.6.5 Prefix in- and decrement operators

本文详细介绍了C#中的前缀增量(++)和减量(--)运算符的使用方法及其内部处理过程。包括这些运算符的操作数类型限制、属性及索引器访问的要求、运算符重载解析等内容,并解释了如何处理前缀形式的增量和减量操作。
pre-increment-expression:
++ unary-expression
pre-decrement-expression:
-- unary-expression
The operand of a prefix increment or decrement operation must be an
expression classified as a variable, a
property access, or an indexer access. The result of the operation is a
value of the same type as the operand.
If the operand of a prefix increment or decrement operation is a property
or indexer access, the property or
indexer must have both a get and a set accessor. If this is not the case, a
compile-time error occurs.
Unary operator overload resolution (?4.2.3) is applied to select a
specific operator implementation.
Predefined ++ and -- operators exist for the following types: sbyte, byte,
short, ushort, int, uint,
long, ulong, char, float, double, decimal, and any enum type. The
predefined ++ operators return
the value produced by adding 1 to the operand, and the predefined --
operators return the value produced
by subtracting 1 from the operand.
The run-time processing of a prefix increment or decrement operation of the
form ++x or --x consists of the
following steps:
?If x is classified as a variable:
x is evaluated to produce the variable.
The selected operator is invoked with the value of x as its argument.
The value returned by the operator is stored in the location given by the
evaluation of x.
The value returned by the operator becomes the result of the operation.
?If x is classified as a property or indexer access:
The instance expression (if x is not static) and the argument list (if x is
an indexer access) associated with
x are evaluated, and the results are used in the subsequent get and set
accessor invocations.
The get accessor of x is invoked.
The selected operator is invoked with the value returned by the get
accessor as its argument.
The set accessor of x is invoked with the value returned by the operator as
its value argument.
The value returned by the operator becomes the result of the operation.
The ++ and -- operators also support postfix notation (?4.5.9). The result
of x++ or x-- is the value of x
before the operation, whereas the result of ++x or --x is the value of x
after the operation. In either case, x
itself has the same value after the operation.
An operator ++ or operator -- implementation can be invoked using either
postfix or prefix notation.
It is not possible to have separate operator implementations for the two
notations.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值