C#中怎么将一个List转换为只读的
时间:2021-03-23 09:34:36|栏目:.NET代码|点击: 次
如题,主要使用AsReadOnly这个方法就可以了
List<int> a = new List<int> {1, 2, 3, 4, 5};
IList<int> b = a.AsReadOnly(); // block modification...
IList<int> c = b.AsWritable(); // ... but unblock it again
c.Add(6);
Debug.Assert(a.Count == 6); // we've modified the original
IEnumerable<int> d = a.Select(x => x); // okay, try this...
IList<int> e = d.AsWritable(); // no, can still get round it
e.Add(7);
复制代码 代码如下:
List<int> a = new List<int> {1, 2, 3, 4, 5};
IList<int> b = a.AsReadOnly(); // block modification...
IList<int> c = b.AsWritable(); // ... but unblock it again
c.Add(6);
Debug.Assert(a.Count == 6); // we've modified the original
IEnumerable<int> d = a.Select(x => x); // okay, try this...
IList<int> e = d.AsWritable(); // no, can still get round it
e.Add(7);
上一篇:c#给图片添加文字的代码小结
栏 目:.NET代码
下一篇:asp.net System.Net.Mail 发送邮件
本文标题:C#中怎么将一个List转换为只读的
本文地址:http://www.codeinn.net/misctech/86298.html