代码检查:从 'readonly' 成员调用非 readonly 成员会导致 'this' 的隐式副本复制发生。
此检查报告了从 readonly 结构体成员调用另一个非 readonly 实例成员的情况。 该调用会导致编译器对 this 进行隐式复制。 此时所有更改都发生在副本上,即使没有更改发生,这个额外的防御性副本也可能令人意外或导致效率低下。
示例
struct S
{
public int Value;
public int GetValue()
{
return Value;
}
public readonly int Read()
{
return GetValue();
}
}
struct S
{
public int Value;
public readonly int GetValue()
{
return Value;
}
public readonly int Read()
{
return GetValue();
}
}
快速修复
根据具体情况,快捷修复可以将被调用成员设为 readonly ,或从包含成员中移除 readonly。
2026年 5月 8日