readonly field can be initialized either at the declaration or in a constructor.‘ data-guid="257637d5ffda9953f70ee09ecc32366c">readonly 字段可以在声明或构造函数中初始化。
class Age
{
readonly int age;
public
Age()
{
age = 10;
}
public void
ChangeAge()
{
//age = 100;//尝试修改age的值,错误
}
}
以下是可以进行的[集合类型不同于普通变量]
class ChuckReadOnly<TKey,TValue>
{
private readonly
Dictionary<TKey, TValue> dictinonary = new Dictionary<TKey,
TValue>();
public void DoTask(TKey key,TValue value)
{
dictinonary.Add(key,value);
}
}
readony主要是监控"="赋值运算的
原文地址:http://www.cnblogs.com/chucklu/p/3772149.html