ref readonly 方法返回的是什么?
它返回一个只读引用,调用方能读取但不能通过该引用修改原值——注意:这不阻止原值被其他途径修改(比如还有普通
ref引用或直接赋值),只约束当前这个引用的写权限。
典型场景是高性能结构体访问,比如从数组或只读集合中“借出”一个
struct的引用,避免复制开销,又防止误改。
怎么声明 ref readonly 返回的方法?
方法签名必须显式写出
ref readonly修饰符,且返回类型前不能加
void或其他修饰;同时,返回的表达式必须本身支持只读引用(比如字段、数组元素、
readonly属性的 backing field)。
ref readonly不能返回局部变量的引用(栈内存会销毁) 不能返回
const或字面量(没有存储地址) 若返回属性,该属性必须是
readonly struct类型,且 getter 内部用
ref readonly返回字段(C# 7.2+ 支持
ref readonlygetter) 数组索引器天然支持
ref readonly,所以
Span<t></t>和
ReadOnlySpan<t></t>的
this[int]就是典型例子
示例:
public ref readonly int GetItem(in int index)
{
return ref _data[index]; // _data 是 int[],index 在范围内
}调用 ref readonly 方法时要注意什么?
接收方必须用
ref readonly声明变量,否则编译失败;也不能把它传给期望
ref或普通值参数的方法。 正确:
ref readonly int item = ref obj.GetItem(0);错误:
ref int item = ref obj.GetItem(0);(权限升级,禁止) 错误:
Console.WriteLine(item);✅ 可读;
item = 42;❌ 编译报错:
Cannot assign to variable 'item' because it is a 'readonly' variable错误:把
ref readonly int传给接受
ref int的方法——类型不兼容
容易被忽略的关键限制
ref readonly不等于“整个对象不可变”,它只冻结当前这一条引用路径。如果原数据是类的字段、或被其他
ref持有,照样可能被改;而且它对
class类型几乎没意义(因为 class 本身是引用类型,
ref readonly MyClass只禁止重新赋值引用,不阻止修改对象内部状态)。
真正安全的只读语义,要配合
readonly struct+
ref readonly+ 不暴露可变字段,三者缺一不可。
