只读字段定义:
不能被赋值
不能被用作ref/out 型参数
只读字段代码示例:
struct Pair
{
public static readonly Pair Origin = new Pair(0,0);
public Pair(int x, int y)
{
this.x = x;
this.y = y;
}
public void Reset()
{
x = 0; //错误
Origin.x = 0; //错误
}
private readonly int x, y;
}