代码检查:结构体成员可以声明为 readonly
此检查会报告结构体中不会修改结构体的实例成员,因此可以标记为 readonly。 添加 readonly 可以明确成员的意图,并且在通过 readonly 引用使用结构体时避免防御性拷贝。
示例
struct Counter
{
private int _value;
public int GetValue()
{
return _value;
}
}
struct Counter
{
private int _value;
public readonly int GetValue()
{
return _value;
}
}
快速修复
为结构体成员添加 readonly 修饰符。
2026年 5月 8日