【发布时间】:2010-09-16 15:58:15
【问题描述】:
我确定我在某处看到我可以通过使用 Init() 方法上方的属性来执行以下操作,它告诉编译器只能从构造函数调用 Init() 方法,从而允许要设置的只读字段。我忘记了这个属性叫什么,我似乎在谷歌上找不到它。
public class Class
{
private readonly int readonlyField;
public Class()
{
Init();
}
// Attribute here that tells the compiler that this method must be called only from a constructor
private void Init()
{
readonlyField = 1;
}
}
【问题讨论】:
标签: c# constructor readonly