C#中怎么将一个List转换为只读的
C#中怎么将一个List转换为只读的
发布时间:2016-12-28 来源:查字典编辑
摘要:如题,主要使用AsReadOnly这个方法就可以了复制代码代码如下:Lista=newList{1,2,3,4,5};IListb=a.As...

如题,主要使用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);

推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
相关阅读
网友关注
最新C#教程学习
热门C#教程学习
编程开发子分类