C# List C#中怎么将一个List转换为只读的

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

C# List C#中怎么将一个List转换为只读的

  2021-03-21 我要评论
想了解C#中怎么将一个List转换为只读的的相关内容吗,在本文为您仔细讲解C# List的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:C#,List,下面大家一起来学习吧。
如题,主要使用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);

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们