软件编程
位置:首页>> 软件编程>> C#编程>> C#中怎么将一个List转换为只读的

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

  发布时间:2021-10-04 15:52:51 

标签: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);
0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com