您的位置:首页 > 编程语言 > C#

C# Readonly 关键字应用在 字典 或 集合

2012-02-19 23:35 471 查看
class Program
{
  static void Main(string[] args)
  {
    tests ts = new tests();
    ts.wrt();
    Console.WriteLine(ts.lst.Count);
    Console.Read();
  }
}

class tests
{
  public readonly int a = 5;
  public readonly Dictionary<string, string> dic=new Dictionary<string,string>();
  public readonly List<string> lst=new List<string>();
  public void wrt()
  {
    a = 10;
    dic["a"] = "";
    lst.Add("a");
  }
}

结果是 1 , 这就是说明 , 即使把 字典 或 集合对象 声明为 readonly , 也是可以对其插入数据的
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: