引用本地重新分配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_operatorWe add the following to the set of assignment_operators.

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.

Ref 赋值运算符生成已分配类型的左值。The ref assignment operator yields an lvalue of the assigned type. 如果左操作数可写 (即不是或) ,则它是可写的 ref readonly inIt is writeable if the left operand is writeable (i.e. not ref readonly or in).

此操作员的安全规则如下:The safety rules for this operator are:

  • 对于 ref 重新分配 e1 = ref e2 ,的 ref 安全到转义e2 范围必须至少与的 引用安全对 的范围相同 e1For a ref reassignment e1 = ref e2, the ref-safe-to-escape of e2 must be at least as wide a scope as the ref-safe-to-escape of e1.

在中,引用 安全到转义 的定义在 安全性上适用于类似引用类型Where ref-safe-to-escape is defined in Safety for ref-like types