参照ローカル再割り当てRef Local Reassignment
C# 7.3 では、ref ローカル変数または ref パラメーターの参照を再バインドするためのサポートが追加されています。In C# 7.3, we add support for rebinding the referent of a ref local variable or a ref parameter.
次のをのセットに追加し assignment_operator
ます。We add the following to the set of assignment_operator
s.
assignment_operator
: '=' 'ref'
;
=ref
演算子は、*ref 代入演算子 _ と呼ばれます。The =ref
operator is called the *ref assignment operator _. これは _compound 代入演算子 * ではありません。It is not a _compound assignment operator*. 左オペランドは、ref ローカル変数、ref パラメーター (以外 this
)、または out パラメーターにバインドする式である必要があります。The left operand must be an expression that binds to a ref local variable, a ref parameter (other than this
), or an out parameter. 右オペランドは、左辺オペランドと同じ型の値を示す左辺値を生成する式である必要があります。The right operand must be an expression that yields an lvalue designating a value of the same type as the left operand.
右オペランドは、ref 割り当ての時点で確実に代入される必要があります。The right operand must be definitely assigned at the point of the ref assignment.
左側のオペランドがパラメーターにバインドされている場合 out
、 out
ref 代入演算子の先頭でそのパラメーターが確実に割り当てられていないと、エラーになります。When the left operand binds to an out
parameter, it is an error if that out
parameter has not been definitely assigned at the beginning of the ref assignment operator.
左側のオペランドが書き込み可能な参照 (つまり、ローカルまたはパラメーター以外の値を指定する) の場合 ref readonly
in
、右オペランドは書き込み可能な左辺値である必要があります。If the left operand is a writeable ref (i.e. it designates anything other than a ref readonly
local or in
parameter), then the right operand must be a writeable lvalue.
参照代入演算子は、割り当てられた型の左辺値を生成します。The ref assignment operator yields an lvalue of the assigned type. 左側のオペランドが書き込み可能である (またはではない) 場合は、書き込み可能です ref readonly
in
。It is writeable if the left operand is writeable (i.e. not ref readonly
or in
).
この演算子の安全性規則は次のとおりです。The safety rules for this operator are:
- 参照の再割り当ての場合
e1 = ref e2
、の ref セーフツーエスケープ は、e2
の ref セーフツーエスケープ と同じ範囲内である必要がありe1
ます。For a ref reassignmente1 = ref e2
, the ref-safe-to-escape ofe2
must be at least as wide a scope as the ref-safe-to-escape ofe1
.
Ref のような相互 にエスケープする と、ref に 似た型の安全性が定義されます。Where ref-safe-to-escape is defined in Safety for ref-like types